0
点赞
收藏
分享

微信扫一扫

c++11std::promise使用

素的盐 2022-02-04 阅读 23
#include <iostream>
#include <string>
#include <thread>
#include <future>
using namespace std;

int run1(std::promise<int> &tmp)
{
	int a = 10;
	tmp.set_value(a);
	std::cout << "子线程ID = " << std::this_thread::get_id() << std::endl;
	return 3;
}
int main()
{
    std::cout << "主线程ID = " << std::this_thread::get_id() << std::endl;
    std::promise<int> tmp;
    std::thread obj(run1, std::ref(tmp));
    obj.join();
    //从线程当中获取一个值,拿到这个值后,可以传递到另一个线程当中。
    std::future<int> a = tmp.get_future();//将std::promise和std::future进行绑定
    std::cout << a.get() << std::endl;
    return 0;
}

在这里插入图片描述

举报

相关推荐

0 条评论