2011-05-24 70 views
2

假设我创建了多个线程。 现在我等待多个对象以及使用:在Windows C++中退出代码线程

WaitOnMultipleObject(...); 

现在,如果我想知道的所有线程的返回码的状态。怎么做 ?

我是否需要循环所有线程的循环句柄。

GetExitCodeThread(
    __in HANDLE hThread, 
    __out LPDWORD lpExitCode 
); 

现在检查成功/失败代码lpExitCode

干杯, 悉达多

回答

2

如果您想等待线程退出,请等待线程的句柄。等待完成后,您可以获取该线程的退出代码。

DWORD result = WaitForSingleObject(hThread, 0); 

if (result == WAIT_OBJECT_0) { 
    // the thread handle is signaled - the thread has terminated 
    DOWRD 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()等待几个线程完成。找出使用WAIT_OBJECT_0WaitForMultipleObjects()返回的适当偏移量完成的线程,并在调用它等待下一个线程完成时从传递到WaitForMultipleObjects()的句柄数组中移除该线程句柄。

3

我需要循环的回路中的所有线程的 手柄。

GetExitCodeThread(
    __in HANDLE hThread, 
    __out LPDWORD lpExitCode 
); 

是的。