/////////////////////////////////////////////////////////////// // Future.cpp - asynchronous call // // // // Jim Fawcett, CSE687 - Object Oriented Design, Spring 2013 // /////////////////////////////////////////////////////////////// #include #include #include void say(const std::string& msg, int millisec) { static std::mutex mtx; for(int i=0; i<25; ++i) { mtx.lock(); std::cout << "\n " << msg.c_str(); mtx.unlock(); std::this_thread::sleep_for(std::chrono::milliseconds(millisec)); } } int main() { auto fut = std::async(say, "hello from async", 2); say("hello from main", 0); fut.wait(); // doesn't assert with no wait, but background doesn't complete std::cout << "\n\n"; }