如何确定Win32线程是否已终止?

如何确定Win32线程是否已终止?

GetExitCodeThread的文档警告不要因为这个原因使用它,因为出于其他原因可以返回错误代码STILL_ACTIVE。

谢谢您的帮助! 🙂

MSDN提到“当线程终止时,线程对象获得信号状态,满足任何等待对象的线程”。

因此,您可以通过检查线程句柄的状态来检查线程是否已终止 – 是否已发出信号:

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 } 

您链接的文档警告不要使用STILL_ACTIVE作为返回代码,因为它无法与用于指示活动线程的返回值区分开来。 因此,不要将它用作返回值,您将不会遇到此问题。