29 thread (std::function<
unsigned int ()> func);
34 virtual void start ();
43 DWORD
wait (DWORD time_limit = INFINITE);
49 DWORD
wait_msg (DWORD time_limit = INFINITE, DWORD mask = QS_ALLINPUT);
75 virtual void name (
const std::string& nam);
79 thread (
const std::string&
name = std::string (), DWORD stack_size = 0,
80 PSECURITY_DESCRIPTOR sd = NULL,
bool inherit =
false);
94 void initialize (PSECURITY_DESCRIPTOR sd, BOOL inherit);
101 static unsigned int _stdcall entryProc (
thread* ts);
103 std::function<
unsigned int ()> thfunc;
104 std::exception_ptr pex;
117 HANDLE handle ()
const;
156 return GetThreadPriority (
handle ());
161 SetThreadPriority (
handle (), pri);
176 std::rethrow_exception (pex);
184 return GetCurrentThreadId ();
187inline HANDLE current_thread::handle ()
const
189 return GetCurrentThread ();
195 return GetThreadPriority (GetCurrentThread ());
201 SetThreadPriority (GetCurrentThread (), pri);
215template <ThreadDerived T>
216inline DWORD
wait_all (
const T* objs,
int count, DWORD msec = INFINITE)
218 assert (count < MAXIMUM_WAIT_OBJECTS);
219 HANDLE harr[MAXIMUM_WAIT_OBJECTS];
220 for (
int i = 0; i < count; i++)
221 harr[i] = objs[i].handle ();
223 DWORD result = WaitForMultipleObjects (count, harr,
true, msec);
224 if (result < WAIT_OBJECT_0 + count)
226 for (
int i = 0; i < count; i++)
227 objs[i].rethrow_exception ();
233template <ThreadDerived T>
234inline DWORD
wait_all (std::initializer_list<const T*> objs, std::chrono::milliseconds limit)
236 assert (objs.size () < MAXIMUM_WAIT_OBJECTS);
237 HANDLE harr[MAXIMUM_WAIT_OBJECTS];
239 for (
auto& th : objs)
240 harr[i++] = th->handle ();
242 DWORD msec = (DWORD)limit.count ();
243 DWORD result = WaitForMultipleObjects ((DWORD)objs.size (), harr,
true, msec);
244 if (result < WAIT_OBJECT_0 + objs.size ())
246 for (
auto& th : objs)
247 th->rethrow_exception ();
253template <ThreadDerived T>
254inline DWORD
wait_all (std::initializer_list<const T*> objs, DWORD msec = INFINITE)
256 assert (objs.size () < MAXIMUM_WAIT_OBJECTS);
257 HANDLE harr[MAXIMUM_WAIT_OBJECTS];
259 for (
auto& th : objs)
260 harr[i++] = th->handle ();
262 DWORD result = WaitForMultipleObjects ((DWORD)objs.size (), harr,
true, msec);
263 if (result < WAIT_OBJECT_0 + objs.size ())
265 for (
auto& th : objs)
266 th->rethrow_exception ();
272template <ThreadDerived T>
273inline DWORD
wait_any (
const T* objs,
int count, DWORD msec = INFINITE)
275 assert (count < MAXIMUM_WAIT_OBJECTS);
276 HANDLE harr[MAXIMUM_WAIT_OBJECTS];
277 for (
int i = 0; i < count; i++)
278 harr[i] = objs[i].handle ();
280 DWORD result = WaitForMultipleObjects (count, harr,
false, msec);
281 if (result < WAIT_OBJECT_0 + count)
283 for (
int i = 0; i < count; i++)
284 objs[i].rethrow_exception ();
289template <ThreadDerived T>
290inline DWORD
wait_any (std::initializer_list<const T*> objs, DWORD msec = INFINITE)
292 assert (objs.size () < MAXIMUM_WAIT_OBJECTS);
293 HANDLE harr[MAXIMUM_WAIT_OBJECTS];
295 for (
auto& th : objs)
296 harr[i++] = th->handle ();
298 DWORD result = WaitForMultipleObjects ((DWORD)objs.size (), harr,
false, msec);
299 if (result < WAIT_OBJECT_0 + objs.size ())
301 for (
auto& th : objs)
302 th->rethrow_exception ();
307template <ThreadDerived T>
308inline DWORD
wait_any (std::initializer_list<const T*> objs, std::chrono::milliseconds timeout)
310 assert (objs.size () < MAXIMUM_WAIT_OBJECTS);
311 HANDLE harr[MAXIMUM_WAIT_OBJECTS];
313 for (
auto& th : objs)
314 harr[i++] = th->handle ();
315 DWORD msec = (DWORD)timeout.count ();
316 DWORD result = WaitForMultipleObjects ((DWORD)objs.size (), harr,
false, msec);
317 if (result < WAIT_OBJECT_0 + objs.size ())
319 for (
auto& th : objs)
320 th->rethrow_exception ();
325template <ThreadDerived T>
326inline DWORD
wait_msg (
const T* objs,
int count,
bool all =
true, DWORD msec = INFINITE,
327 DWORD mask = QS_ALLINPUT)
329 assert (count < MAXIMUM_WAIT_OBJECTS);
330 HANDLE harr[MAXIMUM_WAIT_OBJECTS];
331 for (
int i = 0; i < count; i++)
332 harr[i] = objs[i].handle ();
334 DWORD result = MsgWaitForMultipleObjects (count, harr, all, msec, mask);
335 if (result < WAIT_OBJECT_0 + count)
337 for (
int i = 0; i < count; i++)
338 objs[i].rethrow_exception ();
343template <ThreadDerived T>
344inline DWORD
wait_msg (std::initializer_list<const T*> objs,
bool all =
true, DWORD msec = INFINITE,
345 DWORD mask = QS_ALLINPUT)
347 assert (objs.size () < MAXIMUM_WAIT_OBJECTS);
348 HANDLE harr[MAXIMUM_WAIT_OBJECTS];
350 for (
auto& th : objs)
351 harr[i++] = th->handle ();
353 DWORD result = MsgWaitForMultipleObjects ((DWORD)objs.size (), harr, all, msec, mask);
354 if (result < WAIT_OBJECT_0 + objs.size ())
356 for (
auto& th : objs)
357 th->rethrow_exception ();
Event objects that reset automatically after a successful wait.
Definition event.h:47
Currently executing thread object.
Definition thread.h:114
DWORD id() const
Return ID of current thread.
Definition thread.h:182
int priority()
Return priority current thread.
Definition thread.h:193
Base class for all named synchronization objects.
Definition syncbase.h:27
HANDLE handle() const
Return OS handle of this object.
Definition syncbase.h:54
virtual void wait()
Wait for object to become signaled.
Definition syncbase.h:86
virtual const std::string & name() const
Return object's name.
Definition syncbase.h:60
Wrapper for a Windows thread.
Definition thread.h:15
void fork()
Another name for start () function.
Definition thread.h:124
DWORD wait_msg(DWORD time_limit=INFINITE, DWORD mask=QS_ALLINPUT)
Wait for thread to finish or a message to be queued.
Definition thread.cpp:225
state get_state() const
Return thread's execution status.
Definition thread.h:149
virtual bool init()
Initialization function called before run.
Definition thread.h:164
void rethrow_exception() const
Rethrow an exception usually in the context of another thread.
Definition thread.h:173
bool is_running() const
Return true if thread is running.
Definition thread.h:144
DWORD wait_alertable(DWORD time_limit=INFINITE)
Wait for thread to finish or an APC, or IO completion routine to occur.
Definition thread.cpp:207
virtual void run()
Default run function.
Definition thread.cpp:154
int priority() const
Return thread's priority.
Definition thread.h:154
state
Execution state of a thread.
Definition thread.h:20
@ starting
in the process of starting up
@ finished
execution finished
@ ending
in the process of finishing
UINT result() const
Return thread's exit code.
Definition thread.h:139
virtual ~thread()
Destructor.
Definition thread.cpp:105
void join()
Another name for wait () function.
Definition thread.h:129
DWORD id() const
Return thread's ID.
Definition thread.h:134
virtual void term()
Finalization function called after run.
Definition thread.h:169
virtual void start()
Begin execution of a newly created thread.
Definition thread.cpp:168
unsigned int exitcode
exit code
Definition thread.h:91
virtual const std::string & name() const
Return object's name.
Definition syncbase.h:60
Specialization of wait functions for threads, re-throw any exceptions that might have occurred during...
Definition thread.h:213
DWORD wait_any(const T *objs, int count, DWORD msec=INFINITE)
Wait for multiple objects until any of them becomes signaled.
Definition syncbase.h:244
DWORD wait_all(const T *objs, int count, DWORD msec=INFINITE)
Wait for multiple objects until all become signaled.
Definition syncbase.h:171
DWORD wait_msg(const T *objs, int count, bool all=true, DWORD msec=INFINITE, DWORD mask=QS_ALLINPUT)
Wait for multiple objects or a message to be queued.
Definition syncbase.h:318