c++ - 如何获得 Windows 线程的返回值?

标签 c++ windows multithreading winapi return-value

我只是想知道是否有可能(如果有,如何)在 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/

相关文章:

c++ - 如何存储或转发 CRTP 模板类的类型

c++ - 在qt4中从文件中导入要在UI中填写的条目

c++ - qt windows 包含 boost 线程 header 失败

windows - 从 Windows 用户模式转储文件中识别主机

java - "system-dependent default"线程池是什么?

c++ - 从托管程序集 (C#) 上的非托管进程( native C++)调用时 LoadLibrary() 的预期行为是什么

c# - 声明变量(内存中的确切位置)

django - 找不到 msguniq。 Django 1.8,Windows 7 64 位

python - 如何在 Python 中分析多线程程序?

ios - 如何在 iOS Swift 中进行多线程、并发或并行?