neic-glass3  1.4.6
threadbaseclass.h
Go to the documentation of this file.
1 /*****************************************
2  * This file is documented for Doxygen.
3  * If you modify this file please update
4  * the comments so that Doxygen will still
5  * be able to work.
6  ****************************************/
7 #ifndef THREADBASECLASS_H
8 #define THREADBASECLASS_H
9 
10 #include <baseclass.h>
11 #include <threadstate.h>
12 #include <workstate.h>
13 #include <thread>
14 #include <mutex>
15 #include <string>
16 #include <vector>
17 #include <map>
18 #include <ctime>
19 
20 namespace glass3 {
21 namespace util {
35  public:
43 
62  ThreadBaseClass(std::string threadName, int sleepTimeMS = 100,
63  int numThreads = 1, int checkInterval = 300);
64 
71 
82  virtual bool start();
83 
94  virtual bool stop();
95 
103  int getNumThreads();
104 
117  void setNumThreads(int numThreads);
118 
129  void setThreadHealth(bool health = true);
130 
142  virtual bool healthCheck();
143 
153  void setSleepTime(int sleepTimeMS);
154 
164  int getSleepTime();
165 
176 
187  void setHealthCheckInterval(int interval);
188 
200 
209  bool getTerminate();
210 
219  const std::string& getThreadName();
220 
231  std::time_t getAllLastHealthy(size_t * pThreadId);
232 
243  virtual glass3::util::WorkState work() = 0;
244 
245  protected:
258 
271  void workLoop();
272 
281  void setLastHealthy(std::time_t now);
282 
286  std::vector<std::thread> m_WorkThreads;
287 
293  std::map<std::thread::id, std::atomic<int>> m_ThreadHealthMap;
294 
295  private:
300  std::string m_sThreadName;
301 
307  std::atomic<int> m_iHealthCheckInterval;
308 
313  std::atomic<int> m_iNumThreads;
314 
319  std::atomic<ThreadState> m_WorkThreadsState;
320 
325  std::atomic<int> m_iSleepTimeMS;
326 
327  bool m_bTerminate;
328 
329  // constants
333  static const int k_iHeathCheckIntervalDefault = 30;
334 
338  static const int k_iSleepTimeDefault = 100;
339 
343  static const int k_iNumThreadsDefault = 10;
344 };
345 } // namespace util
346 } // namespace glass3
347 #endif // THREADBASECLASS_H
virtual bool start()
work thread start function
Definition: threadbaseclass.cpp:61
void workLoop()
ThreadBaseClass work loop function.
Definition: threadbaseclass.cpp:242
virtual bool stop()
work thread stop function
Definition: threadbaseclass.cpp:110
std::time_t getAllLastHealthy(size_t *pThreadId)
Function to retrieve the oldest time any of the work threads last updated their health status as heal...
Definition: threadbaseclass.cpp:305
workstate.h
glass3::util::ThreadState getWorkThreadsState()
Function to get the work threads state.
Definition: threadbaseclass.cpp:369
const std::string & getThreadName()
Function to retrieve the name of the work threads.
Definition: threadbaseclass.cpp:402
threadstatus.h
void setWorkThreadsState(glass3::util::ThreadState state)
Function to set threads state.
Definition: threadbaseclass.cpp:364
The neic-glass3 project namespace containing all the classes and functions that make up neic-glass3...
std::map< std::thread::id, std::atomic< int > > m_ThreadHealthMap
A std::map containing the epoch times as std::atomic<double>> that each work thread was last marked a...
Definition: threadbaseclass.h:293
glass3::util::BaseClass class - encapsulates the most basic setup and configuration logic...
Definition: baseclass.h:30
WorkState
glass3::util::WorkState enumeration
Definition: workstate.h:25
int getHealthCheckInterval()
Function to retrieve the work threads health check interval.
Definition: threadbaseclass.cpp:354
void setThreadHealth(bool health=true)
Function to set thread health.
Definition: threadbaseclass.cpp:164
virtual glass3::util::WorkState work()=0
ThreadBaseClass work function.
ThreadState
glass3::util::ThreadState enumeration
Definition: threadstate.h:24
int getSleepTime()
Retrieves the time to sleep between work() calls.
Definition: threadbaseclass.cpp:397
void setNumThreads(int numThreads)
Sets the number of work threads.
Definition: threadbaseclass.cpp:374
void setSleepTime(int sleepTimeMS)
Sets the time to sleep between work() calls.
Definition: threadbaseclass.cpp:392
void setHealthCheckInterval(int interval)
Function to set work threads health check interval.
Definition: threadbaseclass.cpp:349
ThreadBaseClass()
ThreadBaseClass constructor.
Definition: threadbaseclass.cpp:18
~ThreadBaseClass()
ThreadBaseClass destructor.
Definition: threadbaseclass.cpp:53
std::vector< std::thread > m_WorkThreads
the std::vector that contains the work std::thread objects
Definition: threadbaseclass.h:286
virtual bool healthCheck()
work threads check function
Definition: threadbaseclass.cpp:183
bool getTerminate()
Function to retrieve whether the thread should terminate.
Definition: threadbaseclass.cpp:359
glass3::util::ThreadBaseClass class
Definition: threadbaseclass.h:34
int getNumThreads()
Retrieves the number of work threads.
Definition: threadbaseclass.cpp:387
void setLastHealthy(std::time_t now)
Function to set the last time the work thread was healthy.
Definition: threadbaseclass.cpp:407