加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C:std :: thread的简单返回值?

发布时间:2020-12-16 09:10:44 所属栏目:百科 来源:网络整理
导读:使用win32线程,我有直接的GetExitCodeThread(),它给出了线程函数返回的值.我正在为std :: thread(或boost线程)寻找类似的东西 据我所知,这可以通过期货完成,但究竟如何呢? 解决方法 关于C 11期货,请参见 this video tutorial. 明确与线程和期货: #include
使用win32线程,我有直接的GetExitCodeThread(),它给出了线程函数返回的值.我正在为std :: thread(或boost线程)寻找类似的东西
据我所知,这可以通过期货完成,但究竟如何呢?

解决方法

关于C 11期货,请参见 this video tutorial.

明确与线程和期货:

#include <thread>
#include <future>

void func(std::promise<int> && p) {
    p.set_value(1);
}

std::promise<int> p;
auto f = p.get_future();
std::thread t(&func,std::move(p));
t.join();
int i = f.get();

或者使用std :: async(线程和期货的更高级别包装器):

#include <thread>
#include <future>
int func() { return 1; }
std::future<int> ret = std::async(&func);
int i = ret.get();

我无法评论它是否适用于所有平台(它似乎适用于Linux,但不适用于Mac OSX和GCC 4.6.1).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读