Skip to Content
C++ High Performance
book

C++ High Performance

by Viktor Sehr, Björn Andrist
January 2018
Intermediate to advanced
374 pages
9h 53m
English
Packt Publishing
Content preview from C++ High Performance

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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

C++ High Performance - Second Edition

C++ High Performance - Second Edition

Björn Andrist, Viktor Sehr
Advanced C++

Advanced C++

Gazihan Alankus, Olena Lizina, Rakesh Mane, Vivek Nagarajan, Brian Price
C++ In a Nutshell

C++ In a Nutshell

Ray Lischner
C++ Cookbook

C++ Cookbook

D. Ryan Stephens, Christopher Diggins, Jonathan Turkanis, Jeff Cogswell

Publisher Resources

ISBN: 9781787120952Supplemental Content