I have got this problem:
- Find the first element in a list, for which a given condition holds.
Unfortunately, the list is quite long (100.000 elements), and evaluation the condition for each element takes in total about 30 seconds using one single Thread.
Is there a way to cleanly parallelize this problem? I have looked through all the tbb patterns, but could not find any fitting.
UPDATE: for performance reason, I want to stop as early as possible when an item is found and stop processing the rest of the list. That’s why I believe I cannot use parallel_while or parallel_do.
ok, I have done it this way:
tbb::concurrent_bounded_queue<Element> elements.tbb::concurrent_vector<Element> results.boost::thread_group, and create several threads that run this logic:logic to run in parallel:
So when the first element is found, all other threads will stop processing the next time they check
results.empty().It is possible that two or more threads are working on an element for which
slow_and_painfull_checkreturns true, so I just put the result into a vector and deal with this outside of the parallel loop.After all threads in the thread group have finished, I check all elements in the
resultsand use the one that comes first.