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 produces the right result, but runs both foo's serially (whole app runs 4 seconds). Compiled as:
g++ -std=gnu++11 -O2 foo.cc -lpthread
(Ubuntu 12.10 64bit, gcc 4.7.2)
You might need to add a launch policy of std::launch::async
:
std::async(std::launch::async, foo);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With