从std :: thread获取返回码? [英] Get return code from std::thread?

查看:397
本文介绍了从std :: thread获取返回码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
C ++:std :: thread的简单返回值?

Possible Duplicate:
C++: Simple return value from std::thread?

是否有从std :: thread获取返回代码的方法?我有一个返回整数的函数,当线程完成执行后,我希望能够从该函数中获取返回代码.

Is there anyway to get the return code from a std::thread? I have a function which returns a integer, and I want to be able to get the return code from the function when the thread is done executing.

推荐答案

不,那不是std::thread的目的.

相反,请使用async来获取future:

#include <future>

int myfun(double, char, bool);

auto f = std::async(myfun, arg1, arg2, arg3);  // f is a std::future<int>

// ...

int res = f.get();

您可以使用 wait_for 成员函数>(超时为零)以查看结果是否准备就绪.

You can use the wait_for member function of f (with zero timeout) to see if the result is ready.

这篇关于从std :: thread获取返回码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆