假设我创建了多个线程。现在我也在使用以下命令等待多个对象:
WaitOnMultipleObject(...);现在如果我想知道所有线程的返回码的状态。如何做到这一点?
我是否需要在循环中循环所有线程的句柄。
GetExitCodeThread(
__in HANDLE hThread,
__out LPDWORD lpExitCode
);现在检查lpExitCode的成功/失败代码?
干杯,悉达多
发布于 2011-05-25 03:17:35
如果您想等待线程退出,只需等待线程的句柄。等待完成后,您可以获得该线程的退出代码。
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()的句柄数组以等待下一个线程完成时,从该句柄数组中删除该线程句柄。
发布于 2011-05-25 03:17:34
我需要在循环中循环所有线程的句柄吗?
GetExitCodeThread( __in HANDLE hThread,__out LPDWORD lpExitCode );
是。
https://stackoverflow.com/questions/6115614
复制相似问题