voidtask(int a, std::future<int> &b) { // 2. get value only once a = a +b.get();}std::promise<int> p_in;std::future<int> f_in =p_in.get_future();// thread beginstd::threadt(task,1,f_in);// do something else// 1. set valuep_in.set_value(3);
当多个线程的参数要从主线程中获取
voidtask(int a, std::shared_future<int> b) { // 2. get value more times a = a +b.get(); }std::promise<int> p_in;std::shared_future<int> s_in =p_in.get_future();std::threadt(task,1,s_in);std::threadt(task,1,s_in);std::threadt(task,1,s_in);std::threadt(task,1,s_in);// 1. set valuep_in.set_value(3);