std :: async不会并行化任务

Car*_*s00 4 c++ gcc c++11

在此代码段中使用C++ 11 std :: async:

int foo()
{
    ::sleep(2);
    return 123;
}

int main()
{
    future<int> r1(async(foo));
    int r2 = foo();
    cout << r1.get() + r2 << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

它产生了正确的结果,但是连续运行两个foo(整个应用程序运行4秒).编译为: g++ -std=gnu++11 -O2 foo.cc -lpthread(Ubuntu 12.10 64bit,gcc 4.7.2)

Som*_*ude 10

您可能需要添加一个启动策略std::launch::async:

std::async(std::launch::async, foo);
Run Code Online (Sandbox Code Playgroud)