January 2018
Intermediate to advanced
374 pages
9h 53m
English
We can easily use the same divide and conquer concept to implement a parallel version of std::count_if(), with the difference that we need to accumulate the returned value like this:
template <typename It, typename Pred>
auto par_count_if(It first, It last, Pred pred, size_t chunk_sz) {
auto n = static_cast<size_t>(std::distance(first, last));
if (n <= chunk_sz) return std::count_if(first, last, pred);
auto middle = std::next(first, n/2);
auto future = std::async([=, &pred]{
return par_count_if(first, middle, pred, chunk_sz);
});
auto num = par_count_if(middle, last, pred, chunk_sz);
return num + future.get();
}