假设我创建了多个线程。 现在我也在使用以下方式等待多个对象:
WaitOnMultipleObject(...);
现在如果我想知道所有线程的返回代码状态,该怎么做?
我需要在循环中遍历所有线程的句柄吗?
GetExitCodeThread(
__in HANDLE hThread,
__out LPDWORD lpExitCode
);
现在检查lpExitCode
的成功/失败代码?
祝好, Siddhartha
假设我创建了多个线程。 现在我也在使用以下方式等待多个对象:
WaitOnMultipleObject(...);
现在如果我想知道所有线程的返回代码状态,该怎么做?
我需要在循环中遍历所有线程的句柄吗?
GetExitCodeThread(
__in HANDLE hThread,
__out LPDWORD lpExitCode
);
现在检查lpExitCode
的成功/失败代码?
祝好, Siddhartha
如果你想等待一个线程退出,只需等待该线程的句柄。一旦等待完成,您就可以获取该线程的退出代码。
DWORD result = WaitForSingleObject( hThread, INFINITE);
if (result == WAIT_OBJECT_0) {
// the thread handle is signaled - the thread has terminated
DWORD exitcode;
BOOL rc = GetExitCodeThread( hThread, &exitcode);
if (!rc) {
// handle error from GetExitCodeThread()...
}
}
else {
// the thread handle is not signaled - the thread is still alive
}
这个例子可以通过将线程句柄数组传递给WaitForMultipleObjects()
来扩展到等待多个线程的完成。从WaitForMultipleObjects()
的返回值中使用WAIT_OBJECT_0
的适当偏移量确定哪个线程已经完成,并在下一次调用WaitForMultipleObjects()
等待下一个线程完成时从传递给它的句柄数组中删除该线程句柄。
是的。Do I need to loop for all the thread's handle in the loop.
GetExitCodeThread( __in HANDLE hThread, __out LPDWORD lpExitCode );