Search code examples
while-looptbb

Calling tbb::parallel_for inside a while loop


I would like to call tbb::parallel_for inside a while loop with different sets of data. Is my understanding correct that only after all the threads have finished executing in the 1st iteration of the while loop, the parallel_for in the next iteration of the while loop is called. This is because the 2nd iteration of the while loop is dependent on the results of the 1st iteration. Please let me know.

I am thinking that the while loop is sequential in its processing and only after the 1st iteration threads have completed, the next iteration parallel_for is called

Here is some pseudo code: I am using a for loop instead of a while loop

map<int, vector<object*> > objmap;

for(auto objvec : objmap) {
parallel_for(objVec.begin(),objectVec.end(),grainsize),
someObject, simple_partitioner()); }


Solution

  • A tbb::parallel_for completes all iterations before returning, unless it is cancelled, in which case it waits until the cancellation machinery finishes. The enclosing while loop is sequential, so it's safe for the 2nd invocation of the tbb::parallel_for to read results from the 1st invocation.