Implementing parallel std::count_if

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(); 
} 

Get C++ High Performance now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.