TDME2  1.9.200
Thread.h
Go to the documentation of this file.
1 #pragma once
2 
4 
5 #include <tdme/tdme.h>
6 
7 #include <chrono>
8 #include <memory>
9 #include <thread>
10 #include <string>
11 
12 using std::string;
13 using std::thread;
14 using std::unique_ptr;
15 
16 /**
17  * Base class for threads.
18  * @author Andreas Drewke
19  */
21 public:
22  // forbid class copy
24 
25  /**
26  * @brief Public constructor
27  * @param name name
28  * @param autoDelete delete thread after thread execution has been completed
29  */
30  inline Thread(const string& name, bool autoDelete = false): name(name), stopRequested(false), autoDelete(autoDelete) {}
31 
32  /**
33  * @brief Public destructor
34  */
35  inline virtual ~Thread() {}
36 
37  /**
38  * @return hardware thread count
39  */
40  inline static int getHardwareThreadCount() {
41  return std::thread::hardware_concurrency();
42  }
43 
44  /**
45  * @brief sleeps current thread for given time in milliseconds
46  * @param milliseconds milliseconds to wait
47  */
48  inline static void sleep(const uint64_t milliseconds) {
49  std::this_thread::sleep_for(std::chrono::duration<uint64_t, std::milli>(milliseconds));
50  }
51 
52  /**
53  * @brief Blocks caller thread until this thread has been terminated
54  */
55  inline void join() {
56  stlThread.join();
57  }
58 
59  /**
60  * @brief Starts this objects thread
61  */
62  inline virtual void start() {
63  stlThread = thread(threadRun, (void*)this);
64  }
65 
66  /**
67  * @brief Requests that this thread should be stopped
68  */
69  inline virtual void stop() {
70  stopRequested = true;
71  }
72 
73  /**
74  * @brief Returns if stop has been requested
75  * @return bool
76  */
77  inline bool isStopRequested() {
78  return stopRequested;
79  }
80 
81 protected:
82  /**
83  * @brief Abstract run() method, should be implemented by subclassed class, will be called after spawn by start()
84  */
85  virtual void run() = 0;
86 
87 private:
88  inline static void threadRun(void *thread) {
89  auto threadPtr = static_cast<Thread*>(thread);
90  threadPtr->run();
91  if (threadPtr->autoDelete == true) {
92  threadPtr->stlThread.detach();
93  delete threadPtr;
94  }
95  }
96 
97  string name;
98  thread stlThread;
99  volatile bool stopRequested;
101 };
Base class for threads.
Definition: Thread.h:20
virtual void run()=0
Abstract run() method, should be implemented by subclassed class, will be called after spawn by start...
virtual ~Thread()
Public destructor.
Definition: Thread.h:35
static void sleep(const uint64_t milliseconds)
sleeps current thread for given time in milliseconds
Definition: Thread.h:48
virtual void start()
Starts this objects thread.
Definition: Thread.h:62
void join()
Blocks caller thread until this thread has been terminated.
Definition: Thread.h:55
volatile bool stopRequested
Definition: Thread.h:99
static void threadRun(void *thread)
Definition: Thread.h:88
static int getHardwareThreadCount()
Definition: Thread.h:40
bool isStopRequested()
Returns if stop has been requested.
Definition: Thread.h:77
virtual void stop()
Requests that this thread should be stopped.
Definition: Thread.h:69
#define FORBID_CLASS_COPY(CLASS)
Definition: tdme.h:6