Согласно примечанию здесь :
Если std :: future, полученный из std :: async, не перемещен или не связан со ссылкой, деструктор std :: future будет блокироваться в конце полного выражения до завершения асинхронной операции, по существу делая код [...] синхронный
Это именно ваш случай. Вам необходимо позаботиться о возвращении async
.
#include <vector>
#include <future>
#include <iostream>
#include <chrono>
#include <mutex>
int main()
{
auto func = [](int i) {
std::cout<<" i : "<<i<<" thread_id : "<<std::this_thread::get_id()<<"\n";
};
using Handle = std::future<void>; // pretty hard to be figured out automatically
// from lambda, but your function "returns" void
std::vector<Handle> handles;
for(int j=0;j<10;j++)
{
handles.push_back(std::async(std::launch::async, func, j));
//result of std::async is implicitly moved onto the vector
}
return 0;
}
Выход:
i : 6 thread_id : 47713265002240
i : 7 thread_id : 47713267103488
i : 8 thread_id : 47713269204736
i : 9 thread_id : 47713271305984
i : 5 thread_id : 47713262900992
i : 4 thread_id : 47713260799744
i : 3 thread_id : 47713258698496
i : 2 thread_id : 47713256597248
i : 1 thread_id : 47713254496000
i : 0 thread_id : 47713252394752