Using MS VC++ and Boost thread, I have a routine -in fact a Windows procedure- in the main thread, that is called many times. In each call, the routine creates a new thread - I'm planning create the thread object into persistent space via new and attach the result to an global -static- pointer-. The condition es that I need terminate the previous one if in the next call to the reoutine, the previous thread is still running.
I've read: "Just as the lifetime of a file may be different from the lifetime of an iostream object which represents the file, the lifetime of a thread of execution may be different from the thread object which represents the thread of execution. In particular, after a call to join(), the thread of execution will no longer exist even though the thread object continues to exist until the end of its normal lifetime. The converse is also possible; if a thread object is destroyed without join() having first been called, the thread of execution continues until its initial function completes".
Before reinvent the wheel the question: Is there a standard method to detect if the thread is still running? In such case: what is the canonical method to terminate it?
Thanks in advance.
The canonical way to end a thread is through join
or interrupt
, depending on what exactly your thread does.
As far as I know there is no single function that returns whether the thread is still running or not, however you can try calling timed_join
with a 0 wait time:
Returns:
true if *this refers to a thread of execution on entry, and that thread of execution has completed before the call times out, false otherwise.