fw4spl
Worker.hpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2017.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #ifndef __FWTHREAD_WORKER_HPP__
8 #define __FWTHREAD_WORKER_HPP__
9 
10 #include <boost/any.hpp>
11 #include <boost/asio/io_service.hpp>
12 #include <future>
13 
14 #include <fwCore/base.hpp>
15 #include <fwCore/HiResClock.hpp>
16 
17 #include "fwThread/config.hpp"
18 
19 namespace fwThread
20 {
21 typedef std::thread::id ThreadIdType;
22 
24 FWTHREAD_API ThreadIdType getCurrentThreadId();
25 
26 class Timer;
27 
32 class FWTHREAD_CLASS_API Worker : public ::fwCore::BaseObject
33 {
34 public:
35  typedef ::fwCore::HiResClock::HiResClockType PeriodType;
36  typedef std::function< void () > TaskType;
37  typedef ::boost::any ExitReturnType;
38 
39  typedef std::shared_future< ExitReturnType > FutureType;
40 
42 
43  Worker()
44  {
45  }
46 
48  FWTHREAD_API virtual void stop() = 0;
49 
51  virtual void post(TaskType handler) = 0;
52 
64  template< typename R, typename CALLABLE >
65  std::shared_future< R > postTask(CALLABLE f);
66 
68  FWTHREAD_API virtual ThreadIdType getThreadId() const = 0;
69 
71  FWTHREAD_API virtual SPTR(::fwThread::Timer) createTimer() = 0;
72 
78  virtual FutureType getFuture()
79  {
80  return m_future;
81  }
82 
92  FWTHREAD_API virtual void processTasks(PeriodType maxtime) = 0;
93 
100  FWTHREAD_API virtual void processTasks() = 0;
101 
102 protected:
103 
106  FWTHREAD_API static SPTR(Worker) defaultFactory();
107 
109  Worker( const Worker& );
110 
112  Worker& operator=( const Worker& );
113 
115  FutureType m_future;
116 };
117 
118 } //namespace fwThread
119 
120 #include "fwThread/Worker.hxx"
121 
122 #endif //__FWTHREAD_WORKER_HPP__
#define SPTR(_cls_)
Base class for all FW4SPL&#39;s classes.
Definition: BaseObject.hpp:22
FWTHREAD_API ThreadIdType getCurrentThreadId()
Returns the current thread id.
Definition: Worker.cpp:14
#define fwCoreClassDefinitionsWithFactoryMacro(_classinfo_, _parameters_, _factory_)
Generate common construction methods for classes with one factory.
The Timer class provides single-shot or repetitive timers. A Timer triggers a function once after a d...
Definition: Timer.hpp:27
FutureType m_future
Worker&#39;s loop future.
Definition: Worker.hpp:115
This class creates and manages a task loop. The default implementation create a loop in a new thread...
Definition: Worker.hpp:32
This namespace fwThread provides few tools to execute asynchronous tasks on different threads...