#include
#include
#include
#include
using namespace std;
int test_fun(int x)
{
std::this_thread::sleep_for(std::chrono::seconds(2));
return x+100;
}
void print_int (std::future& fut)
{
int x = fut.get();
std::cout << "value: " << x << 'n';
}
int main()
{
//std::future只能用于单线程中调用 ,多线程调用使用std::share_future();
//async会创建线程执行test_fun,任务创建之后,std::async立即返回一个std::future对象
std::future fut = std::async(test_fun, 1011);
std::cout << "please wait"< prom; // create promise
std::future fut2 = prom.get_future(); // engagement with future
std::thread th1 (print_int, std::ref(fut2)); // send future to new thread
prom.set_value(10); // fulfill promise
// (synchronizes with getting the future)
th1.join();
return 0;
}