#include #include #include #include using namespace std; //--------------------------------------------------------------- // Async method //--------------------------------------------------------------- chrono::microseconds asyncMethod(int delay) { auto t1 = chrono::high_resolution_clock::now(); for (int i = 0; i < 4; i++) { cout << " sleep(" << delay << ")" << endl; sleep(delay); } auto t2 = chrono::high_resolution_clock::now(); return chrono::duration_cast(t2 - t1); } //--------------------------------------------------------------- // The (single) main thread starts executing here. //--------------------------------------------------------------- int main(int argc, char **argv) { // Launch async procedure. Returns a future. auto asyncFuture = async(asyncMethod, 1); // now main thread "does something" sleep(1); // Synchronize w/ async procedure by calling get() on the future // that was returned when we launched the async cout << asyncFuture.get().count() << endl; return EXIT_SUCCESS; }