Can each Iteration of a for loop/for_each be done in parallel? (C++11)

asynchronous, c++, c++11, future, parallel-processing

Solution

You can do something like this

for(T& d : data) std::thread(DoTask, d).detach();

Or you can use something more complicated like Intel's Thread Building Blocks and the `parallel_for` (isn't that the name?) function thereof.

Problem

I'm iterating over a vector of structs and processing each struct individually. It looks something like this: ``` for_each(begin(data),end(data),DoTask); //assume "data" is std::vector<DataT> //assume DoTask is a function that takes a DataT by reference ``` The code is significantly slow because DoTask connects to particular websites and analyzes HTML. What would be the best way to speed this up? My goal is to analyze multiple DataTs at the same time. I'm very new to threading, but std::async and std::future look promising.

Original source