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! :)
-
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.– ElmueAug 30, 2018 at 14:15
2 Answers
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.
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
}
南来地,北往的,上班的,下岗的,走过路过不要错过!
======================个性签名=====================
之前认为Apple 的iOS 设计的要比 Android 稳定,我错了吗?
下载的许多客户端程序/游戏程序,经常会Crash,是程序写的不好(内存泄漏?刚启动也会吗?)还是iOS本身的不稳定!!!
如果在Android手机中可以简单联接到ddms,就可以查看系统log,很容易看到程序为什么出错,在iPhone中如何得知呢?试试Organizer吧,分析一下Device logs,也许有用.