如何获取 Windows 线程的返回值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7100441/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 17:53:08  来源:igfitidea点击:

How can you get the return value of a Windows thread?

c++windowsmultithreadingwinapireturn-value

提问by Sefu

I was just wondering if it is possible (and if so, how) to get the return value of a thread in C++ (Windows). I have several threads, and I use WaitForMultipleObjects(...)on them. This waits until a thread is done, and returns the index of said thread, and all is well. However, I want to be able to obtain the return value of the thread that finished using its handle.

我只是想知道是否有可能(如果有,如何)在 C++ (Windows) 中获取线程的返回值。我有几个线程,我使用WaitForMultipleObjects(...)它们。这会一直等到一个线程完成,并返回该线程的索引,一切都很好。但是,我希望能够获得使用完其句柄的线程的返回值。

For example:

例如:

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?
    }
    ...
}

I have tried to use GetExitCodeThread(thread)but I'm assuming this returns a system exit code, as it always gives me a very strange integer. Does anyone know a way, or a workaround?

我尝试使用,GetExitCodeThread(thread)但我假设这会返回一个系统退出代码,因为它总是给我一个非常奇怪的整数。有谁知道方法或解决方法?

回答by Don Reba

GetExitCodeThreadis the correct function. Here is the MSDN description of what it does:

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.

该函数立即返回。如果指定线程未终止且函数成功,则返回状态为 STILL_ACTIVE。如果线程已终止并且函数成功,则返回的状态为以下值之一:

  • 在 ExitThread 或 TerminateThread 函数中指定的退出值。
  • 线程函数的返回值。
  • 线程进程的退出值。

回答by Ajay

The problem with most programmer is that they don't pay attention to compiler warnings. At level 4 (/W4), the compiler produces following warning when returning -1from a function that is returning DWORD:

大多数程序员的问题是他们不注意编译器警告。在级别 4 (/W4),编译器在-1从返回的函数返回时产生以下警告DWORD

warning C4245: 'return' : conversion from 'int' to 'DWORD', signed/unsigned mismatch

warning C4245: 'return' : conversion from 'int' to 'DWORD', signed/unsigned mismatch

回答by shivakumar

You can use C++ 11's threads concepts using std::future.
See an example below.

您可以使用 C++ 11 的线程概念使用std::future.
请参阅下面的示例。

int ReturnFrmThread() {
    return 100;
}

int main() {
    std::future<int> GetRetVal= std::async(ReturnFrmThread);  // Execution of ReturnFrmThread starts
    int answer = GetAnAnswer.get(); // gets value as 100; 
    // Waits until ReturnFrmThread has finished
}

回答by AppDeveloper

you need to check for STILL_ACTIVE for the values one of these threads may still be active

您需要检查 STILL_ACTIVE 的值,这些线程之一可能仍处于活动状态