Cuberite
A lightweight, fast and extensible game server for Minecraft
IsThread.h
Go to the documentation of this file.
1 
2 // IsThread.h
3 
4 // Interfaces to the cIsThread class representing an OS-independent wrapper for a class that implements a thread.
5 
6 /*
7 Usage:
8 To have a new thread, declare a class descending from cIsThread.
9 Then override its Execute() method to provide your thread processing.
10 In the descending class' constructor call the Start() method to start the thread once you're finished with initialization.
11 */
12 
13 
14 
15 
16 
17 #pragma once
18 
19 
20 
21 
22 
23 class cIsThread
24 {
25 public:
26 
27  cIsThread(AString && a_ThreadName);
28  virtual ~cIsThread();
29 
31  void Start(void);
32 
34  void Stop(void);
35 
37  bool IsCurrentThread(void) const { return std::this_thread::get_id() == m_Thread.get_id(); }
38 
39 protected:
40 
42  virtual void Execute(void) = 0;
43 
45  std::atomic<bool> m_ShouldTerminate;
46 
47 private:
48 
50  std::thread m_Thread;
51 
54 
58 
61  void Entrypoint(void);
62 
64  void SetThreadName() const;
65 } ;
std::string AString
Definition: StringUtils.h:11
Definition: Event.h:18
std::atomic< bool > m_ShouldTerminate
The overriden Execute() method should check this value periodically and terminate if this is true.
Definition: IsThread.h:45
cIsThread(AString &&a_ThreadName)
Definition: IsThread.cpp:16
virtual ~cIsThread()
Definition: IsThread.cpp:26
void Stop(void)
Signals the thread to terminate and waits until it's finished.
Definition: IsThread.cpp:48
void Entrypoint(void)
This is the main thread entrypoint.
Definition: IsThread.cpp:66
AString m_ThreadName
The name of the thread, used to aid debugging in IDEs which support named threads.
Definition: IsThread.h:53
std::thread m_Thread
The thread object which holds the created thread for later manipulation.
Definition: IsThread.h:50
bool IsCurrentThread(void) const
Returns true if the thread calling this function is the thread contained within this object.
Definition: IsThread.h:37
cEvent m_Initialisation
The event that is used to wait with the thread's execution until the thread object is fully initializ...
Definition: IsThread.h:57
void Start(void)
Starts the thread; returns without waiting for the actual start.
Definition: IsThread.cpp:35
virtual void Execute(void)=0
This function, overloaded by the descendants, is called in the new thread.
void SetThreadName() const
Sets the name of the current thread to be the name provided in m_ThreadName.
Definition: IsThread.cpp:94