C++ 如何从线程获取返回值?

C++ 如何从线程获取返回值?,c++,multithreading,c++11,C++,Multithreading,C++11,比如说一个函数 int fun(){ static int a = 10; a = a+1; return a; } 上面的函数返回一个整数值 //Without thread obtaining return value #include<iostream> int main() { int var = 0; var = fun(); std::cout << "The value " << value &l

比如说一个函数

int fun(){
    static int a = 10;
    a = a+1;
    return a;
}
上面的函数返回一个整数值

//Without thread obtaining return value
#include<iostream>
int main()
{
    int var = 0;
    var = fun();
    std::cout << "The value " << value << std::endl;
    return 0;
}
//没有线程获取返回值
#包括
int main()
{
int-var=0;
var=fun();

Std::CUT< P>为获取返回值形式的函数,它将在后台运行,您可能需要考虑而不是直接创建<代码> STD::线程< /代码>对象。您可以使用函数模板来启动异步任务。它返回一个<>代码> STD::将来< /Cord>对象,该对象最终将包含T的返回值。他通过了:

auto res = std::async(fun);

// ... optionally do something else

std::cout << res.get() << '\n';
您可能会尝试的是:

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

// ... optionally do something else

th_sum.join();
// calculation is finished, but where is the result?
th\u sum
表示的线程确实计算1和2的和。但是,您无法从关联的
std::thread
对象获得
sum()
的返回值,即结果

相反,您可以做些什么来处理此缺陷,例如,为
sum()
创建一个包装函数,该函数为结果创建一个out参数,而不是返回它:

void sum_outparam(int a, int b, int& res) { res = sum(a, b); }
然后,您可以启动一个新线程来运行此包装器函数,在的帮助下,您将在
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

<> >代码> STD::线程< /C>只是一个简单的C++包装器,允许启动OS线程并等待它完成。
要与调用线程共享返回值,您可以手动为线程提供共享状态,或者使用更高级别的工具,如等。

您可以使用
async
、承诺的未来(双关语)或打包的任务

// future from a packaged_task
std::packaged_task<int()> task(fun); // wrap the function
std::future<int> f1 = task.get_future();  // get a future
std::thread(std::move(task)).detach(); // launch on a thread

// future from an async()
std::future<int> f2 = std::async(std::launch::async, fun);

// future from a promise
std::promise<int> p;
std::future<int> f3 = p.get_future();
std::thread( [](std::promise<int>& p){ p.set_value(fun()); },
             std::ref(p) ).detach();


std::cout << "Waiting...";
f1.wait();
f2.wait();
f3.wait();
std::cout << "Done!\nResults are: "
          << f1.get() << ' ' << f2.get() << ' ' << f3.get() << '\n';
//来自打包任务的未来
std::packated_task(fun);//包装函数
std::future f1=task.get_future();//获取未来
std::thread(std::move(task)).detach();//在线程上启动
//来自异步()的未来
std::future f2=std::async(std::launch::async,fun);
//来自承诺的未来
std::promise p;
std::future f3=p.get_future();
std::thread([](std::promise&p){p.set_值(fun());},
std::ref(p)).detach();

std::cout
int return_of_t1;std::thread t1([&return_of_t1]{return_of_t1=fun();})
顺便说一句,从不同线程调用
fun
将导致最终的混乱。好了,我们可以使用lambda函数,我认为我们可以通过使用锁定机制避免混乱。
std::async
被设计成这样做。可能重复
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
// future from a packaged_task
std::packaged_task<int()> task(fun); // wrap the function
std::future<int> f1 = task.get_future();  // get a future
std::thread(std::move(task)).detach(); // launch on a thread

// future from an async()
std::future<int> f2 = std::async(std::launch::async, fun);

// future from a promise
std::promise<int> p;
std::future<int> f3 = p.get_future();
std::thread( [](std::promise<int>& p){ p.set_value(fun()); },
             std::ref(p) ).detach();


std::cout << "Waiting...";
f1.wait();
f2.wait();
f3.wait();
std::cout << "Done!\nResults are: "
          << f1.get() << ' ' << f2.get() << ' ' << f3.get() << '\n';