How can I determine if a Win32 thread has terminated?
Ask Question Asked 14 years, 2 months agoHow 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! :)
Share Edit asked Nov 19, 2008 at 5:17 Uhall 5,54377 gold badges2424 silver badges1919 bronze badges- If the thread is your own you can make sure that all your threads return 0. So this problem will never arise. I think ExitCodeThread() is the better solution than the accepted answer in case you are needing this for your own threads because the "other reasons" mentioned in the MSDN are simply that YOU return the exit code STILL_ACTIVE from your thread routine. So simply don't do it. – Elmue Aug 30, 2018 at 14:15
2 Answers
Reset to default 7The 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.
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
}
Share
Edit
answered Nov 19, 2008 at 5:45
标签:sheet,thread,share,Win32,How,popover,data,se
From: https://www.cnblogs.com/ioriwellings/p/17087683.html