The Gaudi Framework  v29r0 (ff2e7097)
task_adaptors.hpp
Go to the documentation of this file.
1 
17 #ifndef THREADPOOL_TASK_ADAPTERS_HPP_INCLUDED
18 #define THREADPOOL_TASK_ADAPTERS_HPP_INCLUDED
19 
20 
21 #include <boost/smart_ptr.hpp>
22 #include <boost/function.hpp>
23 #include <boost/thread.hpp>
24 
25 
26 namespace boost { namespace threadpool
27 {
28 
37  typedef function0<void> task_func;
38 
39 
40 
41 
52  {
53  private:
54  unsigned int m_priority;
55  task_func m_function;
56 
57  public:
58  typedef void result_type;
59 
60  public:
65  prio_task_func(unsigned int const priority, task_func const & function)
66  : m_priority(priority)
67  , m_function(function)
68  {
69  }
70 
73  void operator() (void) const
74  {
75  if(m_function)
76  {
77  m_function();
78  }
79  }
80 
85  bool operator< (const prio_task_func& rhs) const
86  {
87  return m_priority < rhs.m_priority;
88  }
89 
90  }; // prio_task_func
91 
92 
93 
94 
95 
96 
97 
98 
108  {
109  private:
110  function0<bool> m_function;
111 #if BOOST_VERSION >= 15000
112  boost::chrono::milliseconds m_break;
113 #else
114  unsigned int m_break_s;
115  unsigned int m_break_ns;
116 #endif
117  public:
118  typedef void result_type;
119 
120  public:
125  looped_task_func(function0<bool> const & function, unsigned int const interval = 0)
126  : m_function(function)
127 #if BOOST_VERSION >= 15000
128  , m_break(interval)
129 #endif
130  {
131 #if BOOST_VERSION < 15000
132  m_break_s = interval / 1000;
133  m_break_ns = (interval - m_break_s * 1000) * 1000 * 1000;
134 #endif
135  }
136 
139  void operator() (void) const
140  {
141  if(m_function)
142  {
143 #if BOOST_VERSION >= 15000
144  if(m_break > boost::chrono::milliseconds(0))
145  boost::this_thread::sleep_for(m_break);
146 #else
147  if(m_break_s > 0 || m_break_ns > 0)
148  {
149  // Sleep some time before first execution
150  xtime xt;
151  xtime_get(&xt, TIME_UTC);
152  xt.nsec += m_break_ns;
153  xt.sec += m_break_s;
154  thread::sleep(xt);
155  }
156 #endif
157  while(m_function())
158  {
159 #if BOOST_VERSION >= 15000
160  if(m_break > boost::chrono::milliseconds(0))
161  boost::this_thread::sleep_for(m_break);
162 #else
163  if(m_break_s > 0 || m_break_ns > 0)
164  {
165  xtime xt;
166  xtime_get(&xt, TIME_UTC);
167  xt.nsec += m_break_ns;
168  xt.sec += m_break_s;
169  thread::sleep(xt);
170  }
171 #endif
172  else
173  {
174  thread::yield(); // Be fair to other threads
175  }
176  }
177  }
178  }
179 
180  }; // looped_task_func
181 
182 
183 } } // namespace boost::threadpool
184 
185 #endif // THREADPOOL_TASK_ADAPTERS_HPP_INCLUDED
186 
task_func m_function
The task&#39;s function.
The namespace threadpool contains a thread pool and related utility classes.
Definition: iter_pos.hpp:13
void result_type
Indicates the functor&#39;s result type.
unsigned int m_priority
The priority of the task&#39;s function.
prio_task_func(unsigned int const priority, task_func const &function)
Constructor.
Looped task function object.
function0< bool > m_function
The task&#39;s function.
bool operator<(const prio_task_func &rhs) const
Comparison operator which realises a partial ordering based on priorities.
void operator()(void) const
Executes the task function.
void result_type
Indicates the functor&#39;s result type.
looped_task_func(function0< bool > const &function, unsigned int const interval=0)
Constructor.
unsigned int m_break_ns
Duration of breaks in nano seconds.
unsigned int m_break_s
Duration of breaks in seconds.
Prioritized task function object.
function0< void > task_func
Standard task function object.