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 // This class will eventually suupersede the old cThread class
6 
7 /*
8 Usage:
9 To have a new thread, declare a class descending from cIsClass.
10 Then override its Execute() method to provide your thread processing.
11 In the descending class' constructor call the Start() method to start the thread once you're finished with initialization.
12 */
13 
14 
15 
16 
17 
18 #pragma once
19 
20 
21 
22 
23 
24 class cIsThread
25 {
26 protected:
29  virtual void Execute(void) = 0;
30 
32  std::atomic<bool> m_ShouldTerminate;
33 
34 public:
35  cIsThread(const AString & a_ThreadName);
36  virtual ~cIsThread();
37 
39  bool Start(void);
40 
42  void Stop(void);
43 
45  bool Wait(void);
46 
48  bool IsCurrentThread(void) const { return std::this_thread::get_id() == m_Thread.get_id(); }
49 
50 private:
51 
54 
56  std::thread m_Thread;
57 
61 
63  void DoExecute(void);
64 } ;
65 
66 
67 
68 
bool Wait(void)
Waits for the thread to finish.
Definition: IsThread.cpp:121
std::atomic< bool > m_ShouldTerminate
The overriden Execute() method should check this value periodically and terminate if this is true...
Definition: IsThread.h:32
void DoExecute(void)
Wrapper for Execute() that waits for the initialization event, to prevent race conditions in thread i...
Definition: IsThread.cpp:70
Definition: Event.h:17
virtual ~cIsThread()
Definition: IsThread.cpp:61
std::thread m_Thread
The thread object which holds the created thread for later manipulation.
Definition: IsThread.h:56
AString m_ThreadName
The name of the thread, used to aid debugging in IDEs which support named threads.
Definition: IsThread.h:53
cEvent m_evtStart
The event that is used to wait with the thread&#39;s execution until the thread object is fully initializ...
Definition: IsThread.h:60
void Stop(void)
Signals the thread to terminate and waits until it&#39;s finished.
Definition: IsThread.cpp:110
std::string AString
Definition: StringUtils.h:13
bool IsCurrentThread(void) const
Returns true if the thread calling this function is the thread contained within this object...
Definition: IsThread.h:48
virtual void Execute(void)=0
This is the main thread entrypoint.
bool Start(void)
Starts the thread; returns without waiting for the actual start.
Definition: IsThread.cpp:80
cIsThread(const AString &a_ThreadName)
Definition: IsThread.cpp:51