BasicThread.h
Go to the documentation of this file.
1 // This file is a part of the OpenSurgSim project.
2 // Copyright 2013, SimQuest Solutions Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #ifndef SURGSIM_FRAMEWORK_BASICTHREAD_H
17 #define SURGSIM_FRAMEWORK_BASICTHREAD_H
18 
19 #include <memory>
20 #include <string>
21 
22 #include <boost/thread.hpp>
23 #include <boost/chrono.hpp>
24 
26 
27 namespace SurgSim
28 {
29 namespace Framework
30 {
31 
32 class Component;
33 class Runtime;
34 
48 {
49 public:
50  explicit BasicThread(const std::string& name = "Unknown Thread");
51 #ifdef _MSC_VER
52  virtual ~BasicThread() throw(...); // Visual Studio does not support noexcept. The throw(...) is optional.
53 #else
54  virtual ~BasicThread() noexcept(false);
55 #endif
56 
59 
67  void start(std::shared_ptr<Barrier> startupBarrier = nullptr, bool isSynchronous = false);
68 
73  void stop();
74 
77  void setIdle(bool isIdle);
78 
81  bool isIdle();
82 
85  bool isInitialized();
86 
89  bool isRunning() const;
90 
92  void operator()();
93 
95  boost::thread& getThread();
96 
98  std::string getName() const;
99 
102  void setRate(double val)
103  {
104  m_period = boost::chrono::duration<double>(1.0 / val);
105  }
106 
115  bool setSynchronous(bool val);
116 
119  bool isSynchronous();
120 
121 protected:
122 
126  bool initialize();
127 
132  bool startUp();
133 
134  bool waitForBarrier(bool success);
135 
136  virtual bool executeInitialization();
137 
138 
139 private:
141 
142  boost::thread m_thisThread;
143  boost::chrono::duration<double> m_period;
144  std::shared_ptr<Barrier> m_startupBarrier;
145 
146  bool m_isIdle;
151 
152  virtual bool doInitialize() = 0;
153  virtual bool doStartUp() = 0;
154 
159  virtual bool doUpdate(double dt);
160 
163  virtual void doBeforeStop();
164 };
165 
166 }; // namespace Framework
167 }; // namespace SurgSim
168 
169 #endif // SURGSIM_FRAMEWORK_BASICTHREAD_H
boost::thread m_thisThread
Definition: BasicThread.h:142
boost::thread & getThread()
Definition: BasicThread.cpp:93
Definition: DriveElementFromInputBehavior.cpp:27
bool m_isSynchronous
Definition: BasicThread.h:150
bool isRunning() const
Query if this object is running.
Definition: BasicThread.cpp:64
bool waitForBarrier(bool success)
Definition: BasicThread.cpp:219
void start(std::shared_ptr< Barrier > startupBarrier=nullptr, bool isSynchronous=false)
C++11 introduced noexcept.
Definition: BasicThread.cpp:81
void stop()
Stopping the execution, blocks until the running thread has actually stopped,.
Definition: BasicThread.cpp:153
std::string getName() const
Definition: BasicThread.cpp:186
void setRate(double val)
Set the update rate of the thread.
Definition: BasicThread.h:102
bool startUp()
Trigger the startup of this object, this will be called after all other threads doInit() was called t...
Definition: BasicThread.cpp:76
virtual bool executeInitialization()
Definition: BasicThread.cpp:191
bool m_isIdle
Definition: BasicThread.h:146
bool m_isRunning
Definition: BasicThread.h:148
virtual void doBeforeStop()
Prepares the thread for its execution to be stopped.
Definition: BasicThread.cpp:247
string(TOUPPER ${DEVICE}DEVICE_UPPER_CASE) option(BUILD_DEVICE_ $
Definition: CMakeLists.txt:35
bool setSynchronous(bool val)
Sets the thread to synchronized execution in concert with the startup barrier, the startup barrier ha...
Definition: BasicThread.cpp:228
BasicThread(const std::string &name="Unknown Thread")
Definition: BasicThread.cpp:31
bool initialize()
Trigger the initialization of this object, this will be called before all other threads doStartup() a...
Definition: BasicThread.cpp:69
virtual ~BasicThread() noexcept(false)
C++11 introduced noexcept.
Definition: BasicThread.cpp:45
virtual bool doUpdate(double dt)
Implementation of actual work function for this thread, this has a default implementation to handle d...
Definition: BasicThread.cpp:242
void operator()()
This is what boost::thread executes on thread creation.
Definition: BasicThread.cpp:98
std::string m_name
Definition: BasicThread.h:140
bool m_isInitialized
Definition: BasicThread.h:147
bool isSynchronous()
Query if this object is synchronized.
Definition: BasicThread.cpp:237
Basic thread implementation, tries to maintain a constant rate, supplies startup an initialization...
Definition: BasicThread.h:47
bool isIdle()
Query if this thread is in idle state or not.
Definition: BasicThread.cpp:181
void setIdle(bool isIdle)
Set/Unset the thread in an idle state (doUpdate() called or not in the update() method) ...
Definition: BasicThread.cpp:176
bool m_stopExecution
Definition: BasicThread.h:149
std::shared_ptr< Barrier > m_startupBarrier
Definition: BasicThread.h:144
boost::chrono::duration< double > m_period
Definition: BasicThread.h:143
bool isInitialized()
Query if this object is initialized.
Definition: BasicThread.cpp:59