如何从线程中获取返回值?

Jay*_*mar 7 c++ multithreading c++11

假设有一个函数,

int fun(){
    static int a = 10;
    a = a+1;
    return a;
}
Run Code Online (Sandbox Code Playgroud)

上面的函数返回一个整数值,

//Without thread obtaining return value
#include<iostream>
int main()
{
    int var = 0;
    var = fun();
    std::cout << "The value " << value << std::endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

现在有没有可能的方法来获取 C++11 线程调用时的返回值,

//Using thread
#include<iostream>
#include<thread>
int main()
{
    std::thread t1(fun);//Invoking thread
    //How to obtain the return value of the thread ?
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

谢谢!

眠りネ*_*ネロク 5

为了获取要在后台运行的函数的返回值,您可能需要考虑std::future而不是直接创建std::thread对象。您可以使用std::async()函数模板来启动异步任务。它返回一个std::future对象,该对象最终将包含所传递函数的返回值:

auto res = std::async(fun);

// ... optionally do something else

std::cout << res.get() << '\n';
Run Code Online (Sandbox Code Playgroud)

也就是说,您std::future<int>通过调用创建一个std::async(func). 然后,当你需要的时候fun()的返回值时,只需调用get()future 的成员函数即可。如果 future 还没有准备好(即,如果它还没有结果),那么线程将阻塞,直到它准备好为止。


为什么不直接使用std::thread

问题在于std::thread是它没有提供直接的机制来传输在其构造时传递的可调用对象的返回值。例如,假设您想启动一个新线程,std::thread使用以下函数计算两个整数的总和:

int sum(int a, int b) { return a + b; }
Run Code Online (Sandbox Code Playgroud)

您可能会尝试的是:

std::thread th_sum(sum, 1, 2);

// ... optionally do something else

th_sum.join();
// calculation is finished, but where is the result?
Run Code Online (Sandbox Code Playgroud)

由 代表的线程th_sum确实计算12的和。但是,您无法sum()从关联对象中获取 的返回值,即结果std::thread

相反,您可以采取的措施来解决此缺陷,例如,为其创建一个sum()具有out 参数的包装函数,而不是返回它:

void sum_outparam(int a, int b, int& res) { res = sum(a, b); }
Run Code Online (Sandbox Code Playgroud)

然后,您可以启动一个新线程来运行此包装函数,并在帮助下std::ref()您将获得以下结果res

int res;
std::thread th_sum(sum_outparam, 1, 2, std::ref(res));

// ... optionally do something else


th_sum.join();
// now, res contains the result
Run Code Online (Sandbox Code Playgroud)