31 thread (std::function<
unsigned int ()> func);
36 virtual void start ();
45 DWORD
wait (DWORD time_limit = INFINITE);
51 DWORD
wait_msg (DWORD time_limit = INFINITE, DWORD mask = QS_ALLINPUT);
77 virtual void name (
const std::string& nam);
81 thread (
const std::string&
name = std::string (), DWORD stack_size = 0,
82 PSECURITY_DESCRIPTOR sd = NULL,
bool inherit =
false);
96 void initialize (PSECURITY_DESCRIPTOR sd, BOOL inherit);
103 static unsigned int _stdcall entryProc (
thread* ts);
105 std::function<
unsigned int ()> thfunc;
106 std::exception_ptr pex;
158 return GetThreadPriority (
handle ());
163 SetThreadPriority (
handle (), pri);
178 std::rethrow_exception (pex);
186 return GetCurrentThreadId ();
191 return GetCurrentThread ();
197 return GetThreadPriority (GetCurrentThread ());
203 SetThreadPriority (GetCurrentThread (), pri);
217template <ThreadDerived T>
218inline DWORD
wait_all (
const T* objs,
int count, DWORD msec = INFINITE)
220 assert (count < MAXIMUM_WAIT_OBJECTS);
221 HANDLE harr[MAXIMUM_WAIT_OBJECTS];
222 for (
int i = 0; i < count; i++)
223 harr[i] = objs[i].handle ();
225 DWORD result = WaitForMultipleObjects (count, harr,
true, msec);
226 if (result < WAIT_OBJECT_0 + count)
228 for (
int i = 0; i < count; i++)
229 objs[i].rethrow_exception ();
235template <ThreadDerived T>
236inline DWORD
wait_all (std::initializer_list<const T*> objs, std::chrono::milliseconds limit)
238 assert (objs.size () < MAXIMUM_WAIT_OBJECTS);
239 HANDLE harr[MAXIMUM_WAIT_OBJECTS];
241 for (
auto& th : objs)
242 harr[i++] = th->handle ();
244 DWORD msec = (DWORD)limit.count ();
245 DWORD result = WaitForMultipleObjects ((DWORD)objs.size (), harr,
true, msec);
246 if (result < WAIT_OBJECT_0 + objs.size ())
248 for (
auto& th : objs)
249 th->rethrow_exception ();
255template <ThreadDerived T>
256inline DWORD
wait_all (std::initializer_list<const T*> objs, DWORD msec = INFINITE)
258 assert (objs.size () < MAXIMUM_WAIT_OBJECTS);
259 HANDLE harr[MAXIMUM_WAIT_OBJECTS];
261 for (
auto& th : objs)
262 harr[i++] = th->handle ();
264 DWORD result = WaitForMultipleObjects ((DWORD)objs.size (), harr,
true, msec);
265 if (result < WAIT_OBJECT_0 + objs.size ())
267 for (
auto& th : objs)
268 th->rethrow_exception ();
274template <ThreadDerived T>
275inline DWORD
wait_any (
const T* objs,
int count, DWORD msec = INFINITE)
277 assert (count < MAXIMUM_WAIT_OBJECTS);
278 HANDLE harr[MAXIMUM_WAIT_OBJECTS];
279 for (
int i = 0; i < count; i++)
280 harr[i] = objs[i].handle ();
282 DWORD result = WaitForMultipleObjects (count, harr,
false, msec);
283 if (result < WAIT_OBJECT_0 + count)
285 for (
int i = 0; i < count; i++)
286 objs[i].rethrow_exception ();
291template <ThreadDerived T>
292inline DWORD
wait_any (std::initializer_list<const T*> objs, DWORD msec = INFINITE)
294 assert (objs.size () < MAXIMUM_WAIT_OBJECTS);
295 HANDLE harr[MAXIMUM_WAIT_OBJECTS];
297 for (
auto& th : objs)
298 harr[i++] = th->handle ();
300 DWORD result = WaitForMultipleObjects ((DWORD)objs.size (), harr,
false, msec);
301 if (result < WAIT_OBJECT_0 + objs.size ())
303 for (
auto& th : objs)
304 th->rethrow_exception ();
309template <ThreadDerived T>
310inline DWORD
wait_any (std::initializer_list<const T*> objs, std::chrono::milliseconds timeout)
312 assert (objs.size () < MAXIMUM_WAIT_OBJECTS);
313 HANDLE harr[MAXIMUM_WAIT_OBJECTS];
315 for (
auto& th : objs)
316 harr[i++] = th->handle ();
317 DWORD msec = (DWORD)timeout.count ();
318 DWORD result = WaitForMultipleObjects ((DWORD)objs.size (), harr,
false, msec);
319 if (result < WAIT_OBJECT_0 + objs.size ())
321 for (
auto& th : objs)
322 th->rethrow_exception ();
327template <ThreadDerived T>
328inline DWORD
wait_msg (
const T* objs,
int count,
bool all =
true, DWORD msec = INFINITE,
329 DWORD mask = QS_ALLINPUT)
331 assert (count < MAXIMUM_WAIT_OBJECTS);
332 HANDLE harr[MAXIMUM_WAIT_OBJECTS];
333 for (
int i = 0; i < count; i++)
334 harr[i] = objs[i].handle ();
336 DWORD result = MsgWaitForMultipleObjects (count, harr, all, msec, mask);
337 if (result < WAIT_OBJECT_0 + count)
339 for (
int i = 0; i < count; i++)
340 objs[i].rethrow_exception ();
345template <ThreadDerived T>
346inline DWORD
wait_msg (std::initializer_list<const T*> objs,
bool all =
true, DWORD msec = INFINITE,
347 DWORD mask = QS_ALLINPUT)
349 assert (objs.size () < MAXIMUM_WAIT_OBJECTS);
350 HANDLE harr[MAXIMUM_WAIT_OBJECTS];
352 for (
auto& th : objs)
353 harr[i++] = th->handle ();
355 DWORD result = MsgWaitForMultipleObjects ((DWORD)objs.size (), harr, all, msec, mask);
356 if (result < WAIT_OBJECT_0 + objs.size ())
358 for (
auto& th : objs)
359 th->rethrow_exception ();
Event objects that reset automatically after a successful wait.
Definition event.h:50
Currently executing thread object.
Definition thread.h:116
HANDLE handle() const
Return handle of current thread.
Definition thread.h:189
DWORD id() const
Return ID of current thread.
Definition thread.h:184
int priority()
Return priority of current thread.
Definition thread.h:195
HANDLE handle() const
Return OS handle of this object.
Definition syncbase.h:53
syncbase()
Default constructor.
Definition syncbase.cpp:37
virtual void wait()
Wait for object to become signaled.
Definition syncbase.h:85
virtual const std::string & name() const
Return object's name.
Definition syncbase.h:59
void fork()
Another name for start () function.
Definition thread.h:126
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:218
state get_state() const
Return thread's execution status.
Definition thread.h:151
virtual bool init()
Initialization function called before run.
Definition thread.h:166
void rethrow_exception() const
Rethrow an exception usually in the context of another thread.
Definition thread.h:175
bool is_running() const
Return true if thread is running.
Definition thread.h:146
DWORD wait_alertable(DWORD time_limit=INFINITE)
Wait for thread to finish or an APC, or IO completion routine to occur.
Definition thread.cpp:200
virtual void run()
Default run function.
Definition thread.cpp:147
int priority() const
Return thread's priority.
Definition thread.h:156
state
Execution state of a thread.
Definition thread.h:22
@ starting
in the process of starting up
Definition thread.h:24
@ running
is running
Definition thread.h:25
@ finished
execution finished
Definition thread.h:27
@ ready
not started
Definition thread.h:23
@ ending
in the process of finishing
Definition thread.h:26
UINT result() const
Return thread's exit code.
Definition thread.h:141
virtual ~thread()
Destructor.
Definition thread.cpp:98
void join()
Another name for wait () function.
Definition thread.h:131
DWORD id() const
Return thread's ID.
Definition thread.h:136
thread(std::function< unsigned int()> func)
Make a thread with the given function body.
Definition thread.cpp:64
virtual void term()
Finalization function called after run.
Definition thread.h:171
virtual void start()
Begin execution of a newly created thread.
Definition thread.cpp:161
unsigned int exitcode
exit code
Definition thread.h:93
virtual const std::string & name() const
Return object's name.
Definition syncbase.h:59
Specialization of wait functions for threads, re-throw any exceptions that might have occurred during...
Definition thread.h:215
Definition of mlib::event class.
DWORD wait_any(const T *objs, int count, DWORD msec=INFINITE)
Wait for multiple objects until any of them becomes signaled.
Definition syncbase.h:243
DWORD wait_all(const T *objs, int count, DWORD msec=INFINITE)
Wait for multiple objects until all become signaled.
Definition syncbase.h:170
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:317