/////////////////////////////////////////////////////////////// // Promise.cpp - getting answers from thread // // // // Jim Fawcett, CSE687 - Object Oriented Design, Spring 2013 // /////////////////////////////////////////////////////////////// #include #include #include #include void getAnAnswer(std::promise* p) { std::cout << "\n thread is thinking"; std::this_thread::sleep_for(std::chrono::milliseconds(100)); p->set_value("The answer is 2"); } int main() { std::promise p; std::future f = p.get_future(); std::thread t(getAnAnswer, &p); std::cout << "\n main is waiting for an answer"; std::cout << "\n " << f.get(); t.join(); std::cout << "\n\n"; }