C++ examples for Async:async
Using async to Call Functions
#include <future> #include <iostream> using namespace std; long long Factorial(unsigned int value) { cout << "ThreadTask thread: " << this_thread::get_id() << endl; return value == 1 ? 1/*from w ww.j av a 2s . c o m*/ : value * Factorial(value - 1); } int main(int argc, char* argv[]) { using namespace chrono; cout << "main thread: " << this_thread::get_id() << endl; auto taskFuture1 = async(Factorial, 3); cout << "Factorial result was " << taskFuture1.get() << endl; auto taskFuture2 = async(launch::async, Factorial, 3); cout << "Factorial result was " << taskFuture2.get() << endl; auto taskFuture3 = async(launch::deferred, Factorial, 3); cout << "Factorial result was " << taskFuture3.get() << endl; auto taskFuture4 = async(launch::async | launch::deferred, Factorial, 3); cout << "Factorial result was " << taskFuture4.get() << endl; return 0; }