从 std::thread 获取返回码? [复制]

2023-11-22

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

有没有办法从 std::thread 获取返回码?我有一个返回整数的函数,我希望能够在线程执行完毕时从该函数获取返回代码。


不,不是这样的std::thread is for.

相反,使用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的成员函数f(零超时)查看结果是否准备好。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从 std::thread 获取返回码? [复制] 的相关文章

随机推荐