Std :: async does not parallelize tasks

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)

+4
source share
1 answer

You may need to add a launch policy to std::launch::async :

 std::async(std::launch::async, foo); 
+10
source

Source: https://habr.com/ru/post/1446662/


All Articles