MIRA
CyclicRunnable.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 by
3  * MetraLabs GmbH (MLAB), GERMANY
4  * and
5  * Neuroinformatics and Cognitive Robotics Labs (NICR) at TU Ilmenau, GERMANY
6  * All rights reserved.
7  *
8  * Contact: info@mira-project.org
9  *
10  * Commercial Usage:
11  * Licensees holding valid commercial licenses may use this file in
12  * accordance with the commercial license agreement provided with the
13  * software or, alternatively, in accordance with the terms contained in
14  * a written agreement between you and MLAB or NICR.
15  *
16  * GNU General Public License Usage:
17  * Alternatively, this file may be used under the terms of the GNU
18  * General Public License version 3.0 as published by the Free Software
19  * Foundation and appearing in the file LICENSE.GPL3 included in the
20  * packaging of this file. Please review the following information to
21  * ensure the GNU General Public License version 3.0 requirements will be
22  * met: http://www.gnu.org/copyleft/gpl.html.
23  * Alternatively you may (at your option) use any later version of the GNU
24  * General Public License if such license has been publicly approved by
25  * MLAB and NICR (or its successors, if any).
26  *
27  * IN NO EVENT SHALL "MLAB" OR "NICR" BE LIABLE TO ANY PARTY FOR DIRECT,
28  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
29  * THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLAB" OR
30  * "NICR" HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * "MLAB" AND "NICR" SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
33  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
34  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
35  * ON AN "AS IS" BASIS, AND "MLAB" AND "NICR" HAVE NO OBLIGATION TO
36  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
37  */
38 
47 #ifndef _MIRA_CYCLICRUNNABLE_H_
48 #define _MIRA_CYCLICRUNNABLE_H_
49 
50 #ifndef Q_MOC_RUN
51 #include <boost/function.hpp>
52 #endif
53 
54 #include <thread/ThreadMonitor.h>
55 #include <utils/Time.h>
56 #include <utils/ToString.h>
57 
58 namespace mira
59 {
60 
62 
82 class CyclicRunnable : boost::noncopyable
83 {
84 public:
92  CyclicRunnable(boost::function<void ()> fn, const std::string& name="") :
93  cycleTime(Duration::seconds(1)),
94  mFunction(fn),
95  mName(name)
96  {
97  if ( mName.empty() )
98  mName = "CyclicRunnable " + toString(this);
99  }
100 
108  CyclicRunnable(boost::function<void ()> fn, const Duration& interval,
109  const std::string& name="") :
110  cycleTime(interval),
111  mFunction(fn),
112  mName(name)
113  {}
114 
119  void operator()()
120  {
122  ThreadMonitor::instance().addThisThread(mName);
123 
124  boost::timed_mutex theMutex;
125  boost::system_time endtime = boost::get_system_time();
126  while ( !boost::this_thread::interruption_requested() )
127  {
128  // calculate the time when we want to call the functor again
129  // (next interval)
130  endtime += cycleTime;
131  mFunction();
132  // scope lock the mutex
133  boost::timed_mutex::scoped_lock lock(theMutex);
134  // we try to lock the mutex again and block until we reach the next
135  // interval time. if the specified time has already passed we go on
136  // immediately.
137  theMutex.timed_lock(endtime);
138  }
140  ThreadMonitor::instance().removeThisThread();
141  }
142 
143 public:
144 
147 
148 private:
149  boost::function<void ()> mFunction;
150  std::string mName;
151 };
152 
154 
155 }
156 
157 #endif
static Type & instance()
Returns a reference to the singleton instance.
Definition: Singleton.h:544
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
A thread monitor, which collects information about the resources of all running threads of the curren...
CyclicRunnable(boost::function< void()> fn, const std::string &name="")
Constructs a CyclicRunnable with given functor.
Definition: CyclicRunnable.h:92
Time and Duration wrapper class.
Contains toString and fromString functions for converting data types to strings and the other way rou...
static bool isDestroyed()
Returns true, if the singleton was already destroyed.
Definition: Singleton.h:552
std::string toString(const T &value, int precision=-1)
Converts any data type to string (the data type must support the stream << operator).
Definition: ToString.h:256
CyclicRunnable(boost::function< void()> fn, const Duration &interval, const std::string &name="")
Constructs a CyclicRunnable with given functor and interval.
Definition: CyclicRunnable.h:108
This class extends threads by the ability to execute an operation repeatedly in an defined interval...
Definition: CyclicRunnable.h:82
Use this class to represent time durations.
Definition: Time.h:106
void operator()()
Bind this to a thread to execute the given functor in the specified interval.
Definition: CyclicRunnable.h:119
Duration cycleTime
The cycle time, this cyclic runnable calls its assigned function.
Definition: CyclicRunnable.h:146