// communicating data using std::promise and std::future #include #include #include using data_t = double; void prod(std::promise&& prom, data_t a, data_t b){ prom.set_value(a*b); } int main(){ // promises std::promise prom1, prom2; // futures auto fu_res1 = prom1.get_future(); auto fu_res2 = prom2.get_future(); // calculate the result in two threads data_t a, b; a = 1.0; b = 2.0; std::thread th1(prod,std::move(prom1),a,b); a = 10.0; b = 20.0; std::thread th2(prod,std::move(prom2),a,b); // retreive the result std::cout << fu_res1.get() << "\n"; std::cout << fu_res2.get() << "\n"; th1.join(); th2.join(); }