views:

571

answers:

2

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

+3  A: 

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.

Shog9
Doh!!! I totally read that wrong! Thanks for clearing that up!
Uhall
+14  A: 

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
}
Michael Burr