2 This file is a part of the JThread package, which contains some object-
3 oriented thread wrappers for different thread implementations.
5 Copyright (c) 2000-2006 Jori Liesenborgs (jori.liesenborgs@gmail.com)
7 Permission is hereby granted, free of charge, to any person obtaining a
8 copy of this software and associated documentation files (the "Software"),
9 to deal in the Software without restriction, including without limitation
10 the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 and/or sell copies of the Software, and to permit persons to whom the
12 Software is furnished to do so, subject to the following conditions:
14 The above copyright notice and this permission notice shall be included in
15 all copies or substantial portions of the Software.
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 DEALINGS IN THE SOFTWARE.
26 #ifndef THREADING_THREAD_H
27 #define THREADING_THREAD_H
29 #include "threading/atomic.h"
30 #include "threading/mutex.h"
33 #if __cplusplus >= 201103L
39 THREAD_PRIORITY_LOWEST,
40 THREAD_PRIORITY_BELOW_NORMAL,
41 THREAD_PRIORITY_NORMAL,
42 THREAD_PRIORITY_ABOVE_NORMAL,
43 THREAD_PRIORITY_HIGHEST,
51 Thread(const std::string &name="Unnamed");
52 virtual ~Thread() { kill(); }
55 inline void stop() { request_stop = true; }
58 inline bool isRunning() { return running; }
59 inline bool stopRequested() { return request_stop; }
60 void *getReturnValue() { return running ? NULL : retval; }
63 static unsigned int getNumberOfProcessors();
64 bool bindToProcessor(unsigned int);
65 bool setPriority(int);
68 * Wait for thread to finish.
69 * Note: this does not stop a thread, you have to do this on your own.
70 * Returns immediately if the thread is not started.
74 static void setName(const std::string &name);
79 virtual void *run() = 0;
82 void setName() { setName(name); }
85 Atomic<bool> request_stop;
89 #if __cplusplus >= 201103L
90 static void theThread(Thread *th);
93 std::thread::native_handle_type getThreadHandle() const
94 { return thread->native_handle(); }
96 # if defined(WIN32) || defined(_WIN32_WCE)
99 static DWORD WINAPI theThread(void *param);
102 static UINT __stdcall theThread(void *param);
106 HANDLE getThreadHandle() const { return thread; }
108 static void *theThread(void *param);
111 pthread_t getThreadHandle() const { return thread; }
113 Atomic<bool> started;