How can I determine if a Win32 thread has terminated?
The documentation for GetExitCodeThread warns to not to use it for this reason since the error code STILL_ACTIVE can be returned for other reasons.
Thanks for the help! :)
When a thread terminates, its termination status changes from STILL_ACTIVE to the exit code of the thread. When a thread terminates, the state of the thread object changes to signaled, releasing any other threads that had been waiting for the thread to terminate.
You can terminate thread by using TerminateThread() using the thread handle you got from CreateThread. PS:It is mentioned in website: "TerminateThread is a dangerous function that should only be used in the most extreme cases.
The C++11 does not have direct method to terminate the threads. The std::future<void> can be used to the thread, and it should exit when value in future is available. If we want to send a signal to the thread, but does not send the actual value, we can pass void type object.
MSDN mentions that "When a thread terminates, the thread object attains a signaled state, satisfying any threads that were waiting on the object".
So, you can check for whether a thread has terminated by checking the state of the thread handle - whether it's signaled or not:
DWORD result = WaitForSingleObject( hThread, 0);
if (result == WAIT_OBJECT_0) {
// the thread handle is signaled - the thread has terminated
}
else {
// the thread handle is not signaled - the thread is still alive
}
The documentation you link to warns against using STILL_ACTIVE
as a return code, since it can't be distinguished from the return value used to indicate an active thread. So don't use it as a return value and you won't have this problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With