I have a loop over a set where I have to do an expensive calculation. I want to do this in parallel using a future class. As far as I understand, async starts a thread or snoozes it and only starts it when I call get () or wait (). Therefore, when I have threads that are not started and trying to get a result, I block the main thread, getting sequential processing. Is there a way to start the remaining pending processes, so everything is calculated in parallel and will not be blocked when get () is called.
std::vector<std::future<class>> futureList;
for (auto elem : container)
{
futureList.push_back(std::async(fct, elem));
}
for (auto elem : futureList)
{
processResult(elem.get())
}
Thank you for your help.
source
share