Using C ++ 11 std :: async in this snippet:
int foo() { ::sleep(2); return 123; } int main() { future<int> r1(async(foo)); int r2 = foo(); cout << r1.get() + r2 << endl; return 0; }
It gives the correct result, but it works like foo in series (the whole application runs for 4 seconds). Compiled as: g++ -std=gnu++11 -O2 foo.cc -lpthread (Ubuntu 12.10 64bit, gcc 4.7.2)
source share