Simplifying construction via a wrapper

In order to iterate the indices with a neat syntax the previous code is wrapped into a utility function named parallel_for() as shown below:

template <typename Policy, typename Index, typename F>auto parallel_for(Policy p, Index first, Index last, F f) {  auto r = make_linear_range<Index>(first, last, last);  std::for_each(std::move(p), r.begin(), r.end(), std::move(f));}

The parallel_for() can then be used like this:

parallel_for(std::execution::par, size_t{0}, mice.size(), [&](size_t i){ 
  if (idx == 0) mice[i] += " is first."; 
  else if (i + 1 == mice.size()) mice[i] += " is last."; 
});
for(const auto& m: mice)   std::cout << m << ', ';// Output: Mickey is first, Minnie, Jerry is last,

As the parallel_for ...

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.