Newton Dynamics  4.00
ndThread.h
1 /* Copyright (c) <2003-2022> <Julio Jerez, Newton Game Dynamics>
2 *
3 * This software is provided 'as-is', without any express or implied
4 * warranty. In no event will the authors be held liable for any damages
5 * arising from the use of this software.
6 *
7 * Permission is granted to anyone to use this software for any purpose,
8 * including commercial applications, and to alter it and redistribute it
9 * freely, subject to the following restrictions:
10 *
11 * 1. The origin of this software must not be misrepresented; you must not
12 * claim that you wrote the original software. If you use this software
13 * in a product, an acknowledgment in the product documentation would be
14 * appreciated but is not required.
15 *
16 * 2. Altered source versions must be plainly marked as such, and must not be
17 * misrepresented as being the original software.
18 *
19 * 3. This notice may not be removed or altered from any source distribution.
20 */
21 
22 #ifndef __ND_THREAD_H_
23 #define __ND_THREAD_H_
24 
25 #include "ndCoreStdafx.h"
26 #include "ndSemaphore.h"
27 #include "ndClassAlloc.h"
28 
30 {
31  public:
32  ndThreadName()
33  {
34  strcpy(m_name, "newtonWorker");
35  }
36  char m_name[32];
37 };
38 
40 class ndThread
41  :public ndClassAlloc
42  ,public ndThreadName
43  ,public ndSemaphore
44 #ifndef D_USE_THREAD_EMULATION
45  ,public ndAtomic<bool>
46  ,public std::condition_variable
47  ,public std::thread
48 #endif
49 {
50  public:
53  D_CORE_API ndThread();
54 
57  D_CORE_API virtual ~ndThread();
58 
61  D_CORE_API void SetName(const char* const name);
62 
64  D_CORE_API void Signal();
65 
69  D_CORE_API void Finish();
70 
75  virtual void ThreadFunction() = 0;
76 
77  protected:
78  virtual void Release(){}
79 
80  private:
81  void ThreadFunctionCallback();
82 };
83 
84 #endif
ndClassAlloc
Base class for providing memory allocation for all other engine classes.
Definition: ndClassAlloc.h:30
ndThread
Base class for for all multi thread functionality.
Definition: ndThread.h:49
ndThreadName
Definition: ndThread.h:30
ndThread::ndThread
D_CORE_API ndThread()
Empty default constructor after creation all threads go to a wait state.
Definition: ndThread.cpp:33
ndThread::SetName
D_CORE_API void SetName(const char *const name)
Set thread name.
Definition: ndThread.cpp:57
ndThread::ThreadFunction
virtual void ThreadFunction()=0
Thread function to execute in a perpetual loop until the thread is terminated.
ndThread::~ndThread
virtual D_CORE_API ~ndThread()
Empty, does not terminate the thread loop.
Definition: ndThread.cpp:53
ndThread::Finish
D_CORE_API void Finish()
Force the thread loop to terminate.
Definition: ndThread.cpp:90
ndSemaphore
Generic counting semaphore for thread synchronization.
Definition: ndSemaphore.h:29
ndThread::Signal
D_CORE_API void Signal()
Set the thread, to execute one call to and go back to a wait state
Definition: ndThread.cpp:98
ndAtomic
wrapper over standard atomic operations
Definition: ndTypes.h:381