C++11>future,promise

懵懂的女人 提交于 2020-01-18 23:56:22

future


// promise example
#include <iostream>       // std::cout
#include <functional>     // std::ref
#include <thread>         // std::thread
#include <future>         // std::promise, std::future

void print_int(std::future<int>& fut) {
	int x = fut.get();
	std::cout << "value: " << x << '\n';
	std::system("pause");
}

int main()
{
	std::promise<int> prom;                      // create promise     #创建promise
	std::future<int> fut = prom.get_future();    // engagement with future #与future绑定
	std::thread th1(print_int, std::ref(fut));  // send future to new thread  #将future传入promise
	prom.set_value(10);                         // fulfill promise  #执行promise
												// (synchronizes with getting the future) 
	th1.join();
	return 0;
}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!