我只是想知道是否有可能(如果有,如何)在 C++ (Windows) 中获取线程的返回值。我有几个线程,我对它们使用 WaitForMultipleObjects(...)
。这会等待一个线程完成,并返回所述线程的索引,一切都很好。但是,我希望能够获得结束使用其句柄的线程的返回值。
例如:
DWORD WINAPI Thread1(void *parameter){
...
if(...) return 0;
else return -1;
}
DWORD WINAPI Thread2(void *parameter){
...
if(...) return 1;
else return 0;
}
int main(){
HANDLE t1 = CreateThread(0, 0, Thread1, NULL, 0, 0);
HANDLE t2 = CreateThread(0, 0, Thread2, NULL, 0, 0);
HANDLE *threads = new HANDLE[2];
threads[0] = t1;
threads[1] = t2;
int result = WaitForMultipleObjects(2, threads, false, INFINITE);
if(result == 0){
//get the threads value here:
int retVal = SomeFunction(t1); //What is SomeFunction?
}
...
}
我曾尝试使用 GetExitCodeThread(thread)
但我假设这会返回一个系统退出代码,因为它总是给我一个非常奇怪的整数。有谁知道一种方法或解决方法?
最佳答案
GetExitCodeThread是正确的功能。这是它的作用的 MSDN 描述:
This function returns immediately. If the specified thread has not terminated and the function succeeds, the status returned is STILL_ACTIVE. If the thread has terminated and the function succeeds, the status returned is one of the following values:
- The exit value specified in the ExitThread or TerminateThread function.
- The return value from the thread function.
- The exit value of the thread's process.
关于c++ - 如何获得 Windows 线程的返回值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7100441/