C++ examples for Async:Future
Using a packaged_task
#include <future> #include <iostream> using namespace std; long long Factorial(unsigned int value) { this_thread::sleep_for(chrono::seconds(2)); return value == 1 ? 1/*ww w. j a v a 2 s .co m*/ : value * Factorial(value - 1); } int main(int argc, char* argv[]) { using namespace chrono; packaged_task<long long(unsigned int)> task{ Factorial }; future<long long> taskFuture{ task.get_future() }; thread taskThread{ std::move(task), 3 }; while (taskFuture.wait_until(system_clock::now() + seconds(1)) != future_status::ready) { cout << "Still Waiting!" << endl; } cout << "Factorial result was " << taskFuture.get() << endl; taskThread.join(); return 0; }