Browse - Programming Tips - How can I tell if a thread has stopped?

Date: 1997dec17
Updated: 2018dec22
Platform: win32
Language: C/C++
Keywords: stop, halt, alive, dead

Q.  How can I tell if a thread has stopped?

A.  If you have the thread's handle you can do this:

BOOL IsThreadActive(const HANDLE hThread) { if (hThread == NULL) return FALSE; return WaitForSingleObject(hThread, 0) != WAIT_OBJECT_0; }
// Another way BOOL IsThreadActive2(const HANDLE hThread) { if (hThread == NULL) return FALSE; DWORD dwCode = 0; (void) ::GetExitCodeThread(hThread, &dwCode); return (dwCode == STILL_ACTIVE); // STILL_ACTIVE is 259 so the thread // had better not use that as an exit code! }