How can I determine if a Win32 thread has terminated?

21,972

Solution 1

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
}

Solution 2

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.

Share:
21,972
Uhall
Author by

Uhall

Updated on January 20, 2020

Comments

  • Uhall
    Uhall over 4 years

    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! :)