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 
27 
28 namespace SurgSim
29 {
30 namespace Framework
31 {
32 
33 class Component;
34 class Runtime;
35 
49 {
50 public:
51  explicit BasicThread(const std::string& name = "Unknown Thread");
52 #ifdef _MSC_VER
53  virtual ~BasicThread() throw(...); // Visual Studio does not support noexcept. The throw(...) is optional.
54 #else
55  virtual ~BasicThread() noexcept(false);
56 #endif
57 
60 
68  void start(std::shared_ptr<Barrier> startupBarrier = nullptr, bool isSynchronous = false);
69 
74  void stop();
75 
78  void setIdle(bool isIdle);
79 
82  bool isIdle();
83 
86  bool isInitialized();
87 
90  bool isRunning() const;
91 
93  void operator()();
94 
96  boost::thread& getThread();
97 
99  std::string getName() const;
100 
103  void setRate(double val)
104  {
105  m_period = boost::chrono::duration<double>(1.0 / val);
106  }
107 
116  bool setSynchronous(bool val);
117 
120  bool isSynchronous();
121 
125  double getCpuTime() const;
126 
129  size_t getUpdateCount() const;
130 
133 
134 protected:
135 
138 
142  bool initialize();
143 
148  bool startUp();
149 
150  bool waitForBarrier(bool success);
151 
152  virtual bool executeInitialization();
153 
155  std::shared_ptr<SurgSim::Framework::Logger> m_logger;
156 
157 private:
158  std::string m_name;
159 
160  boost::thread m_thisThread;
161  boost::chrono::duration<double> m_period;
162  std::shared_ptr<Barrier> m_startupBarrier;
163 
164  // Protects the start and stop functions so on can only execute once the other is done
165  boost::mutex m_mutexStartStop;
166 
167  bool m_isIdle;
172 
173  virtual bool doInitialize() = 0;
174  virtual bool doStartUp() = 0;
175 
180  virtual bool doUpdate(double dt);
181 
184  virtual void doBeforeStop();
185 };
186 
187 }; // namespace Framework
188 }; // namespace SurgSim
189 
190 #endif // SURGSIM_FRAMEWORK_BASICTHREAD_H
boost::thread m_thisThread
Definition: BasicThread.h:160
boost::thread & getThread()
Definition: BasicThread.cpp:102
Definition: CompoundShapeToGraphics.cpp:29
bool m_isSynchronous
Definition: BasicThread.h:171
bool waitForBarrier(bool success)
Definition: BasicThread.cpp:256
void start(std::shared_ptr< Barrier > startupBarrier=nullptr, bool isSynchronous=false)
C++11 introduced noexcept.
Definition: BasicThread.cpp:87
void stop()
Stopping the execution, blocks until the running thread has actually stopped,.
Definition: BasicThread.cpp:190
void setRate(double val)
Set the update rate of the thread.
Definition: BasicThread.h:103
bool startUp()
Trigger the startup of this object, this will be called after all other threads doInit() was called t...
Definition: BasicThread.cpp:82
std::shared_ptr< SurgSim::Framework::Logger > m_logger
Logger for this thread.
Definition: BasicThread.h:155
virtual bool executeInitialization()
Definition: BasicThread.cpp:228
Timer class, measures execution times.
Definition: Timer.h:30
std::string getName() const
Definition: BasicThread.cpp:223
bool m_isIdle
Definition: BasicThread.h:167
bool m_isRunning
Definition: BasicThread.h:169
virtual void doBeforeStop()
Prepares the thread for its execution to be stopped.
Definition: BasicThread.cpp:299
bool setSynchronous(bool val)
Sets the thread to synchronized execution in concert with the startup barrier, the startup barrier ha...
Definition: BasicThread.cpp:265
BasicThread(const std::string &name="Unknown Thread")
Definition: BasicThread.cpp:31
bool isRunning() const
Query if this object is running.
Definition: BasicThread.cpp:70
bool initialize()
Trigger the initialization of this object, this will be called before all other threads doStartup() a...
Definition: BasicThread.cpp:75
virtual ~BasicThread() noexcept(false)
C++11 introduced noexcept.
Definition: BasicThread.cpp:51
Timer m_timer
Timer to measure the actual time taken to doUpdate.
Definition: BasicThread.h:137
virtual bool doUpdate(double dt)
Implementation of actual work function for this thread, this has a default implementation to handle d...
Definition: BasicThread.cpp:294
void operator()()
This is what boost::thread executes on thread creation.
Definition: BasicThread.cpp:107
std::string m_name
Definition: BasicThread.h:158
boost::mutex m_mutexStartStop
Definition: BasicThread.h:165
bool m_isInitialized
Definition: BasicThread.h:168
bool isSynchronous()
Query if this object is synchronized.
Definition: BasicThread.cpp:274
size_t getUpdateCount() const
Definition: BasicThread.cpp:284
Basic thread implementation, tries to maintain a constant rate, supplies startup an initialization...
Definition: BasicThread.h:48
bool isIdle()
Query if this thread is in idle state or not.
Definition: BasicThread.cpp:218
void setIdle(bool isIdle)
Set/Unset the thread in an idle state (doUpdate() called or not in the update() method) ...
Definition: BasicThread.cpp:213
double getCpuTime() const
Definition: BasicThread.cpp:279
bool m_stopExecution
Definition: BasicThread.h:170
std::shared_ptr< Barrier > m_startupBarrier
Definition: BasicThread.h:162
boost::chrono::duration< double > m_period
Definition: BasicThread.h:161
bool isInitialized()
Query if this object is initialized.
Definition: BasicThread.cpp:65
void resetCpuTimeAndUpdateCount()
Reset the cpu time and the update count to 0.
Definition: BasicThread.cpp:289