ifw  0.0.1-dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Thread.hpp
Go to the documentation of this file.
1 
5 #ifndef IFW_CTD_MPTK_THREAD_HPP_
6 #define IFW_CTD_MPTK_THREAD_HPP_
7 
8 #include <string>
9 #include <thread>
10 
11 #include "rad/Logger.hpp"
12 #include "rad/Exceptions.hpp"
13 
14 #include "ctd/mptk/MessageBus.hpp"
15 
16 namespace ctd {
17 namespace mptk {
18 
20 class Thread {
21 public:
22  static std::map<std::string, Thread*> s_thread_registry;
23  static const std::string THREAD_REGISTRY;
24 
26  typedef enum {
31 
37  Thread(const std::string& thread_id,
38  MessageBus& message_bus,
39  const double period = 0.1);
40 
41  virtual ~Thread();
42 
44  std::string Id();
45 
47  void Loop();
48 
54  virtual void UserLogic();
55 
57  void Start();
58 
60  void Stop();
61 
63  void Pause();
64 
72  bool CheckExecFlag();
73 
76 
78  bool Terminated() const;
79 
81  MessageBus& MsgBus();
82 
84  std::string Print() const;
85 
86 private:
87  std::string m_thread_id;
88  double m_period;
89  ThreadExecControl m_exec_flag;
90  double m_start_time;
91  double m_previous_start_time;
92  double m_next_execution;
93  bool m_thread_terminated;
94 
95  std::shared_ptr<std::thread> m_sys_thread;
96  MessageBus* m_message_bus;
97 };
98 
99 } // namespace mptk
100 } // namespace ctd
101 
102 #endif // IFW_CTD_MPTK_THREAD_HPP_
103 
std::string Id()
Return the thread ID.
Definition: Thread.cpp:53
ThreadExecControl GetExecFlag() const
Return the value of the Thread Execution Flag.
Definition: Thread.cpp:85
static const std::string THREAD_REGISTRY
Definition: Thread.hpp:23
bool Terminated() const
Returns true if thread no longer running.
Definition: Thread.cpp:138
MessageBus & MsgBus()
Get acces to the MessageBus associated with this thread object.
Definition: Thread.cpp:132
ThreadExecControl
Thread execution control/status.
Definition: Thread.hpp:26
bool CheckExecFlag()
Check the Thread Execution Flag. The Thread Execution Flag shall be called regularly in the thread...
Definition: Thread.cpp:113
IFW CTD Multiprocessing Toolkit Message Bus.
Definition: MessageBus.hpp:22
void Loop()
Method to invoke the user provided business logic of the thread.
Definition: Thread.cpp:59
Definition: Thread.hpp:29
void Pause()
Pause the thread execution.
Definition: Thread.cpp:107
static std::map< std::string, Thread * > s_thread_registry
Definition: Thread.hpp:22
void Start()
Start the thread execution.
Definition: Thread.cpp:91
virtual void UserLogic()
User provided business logic. The UserLogic method need not execute an internal loop. It is taken care of by the Control() method. Instead the UserLogic() method should (normally) execute it business logic, and return control to the Control() method, which will take of the thread management.
Definition: Thread.cpp:78
virtual ~Thread()
Definition: Thread.cpp:47
Thread(const std::string &thread_id, MessageBus &message_bus, const double period=0.1)
Constructor method, setting up the internal members.
Definition: Thread.cpp:26
void Stop()
Stop the thread execution.
Definition: Thread.cpp:101
std::string Print() const
Generate ASCII output providing a status of the object.