Sequenced policy

The sequenced execution policy, std::execution::seq, makes the algorithm execute sequentially with no parallelism, just as the algorithm would execute if invoked without any execution policy at all. It might seem odd that this policy is even provided, but it allows the programmer to specify an algorithm to execute sequentially if the number of elements is below a certain threshold where executing an algorithm in parallel is slower:

auto find_largest(const std::vector<int>& v) { 
  auto threshold = 2048; 
  return v.size() < threshold ?     *std::max_element(std::execution::seq, v.begin(), v.end()) :     *std::max_element(std::execution::par, v.begin(), v.end());}

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.