Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Concurrency

Parallel algorithms in C++

Use C++17 execution policies (seq, par, par_unseq, unseq) with for_each, transform, reduce and sort, and learn the rules your callbacks must follow.

Since C++17, most standard algorithms accept an optional first argument, an execution policy, that allows the library to run the work on several threads and/or with SIMD instructions. The policies live in <execution>: std::execution::seq, par, par_unseq and (C++20) unseq. Everything else about the call stays the same, so std::sort(v.begin(), v.end()) becomes std::sort(std::execution::par, v.begin(), v.end()).

A policy is a permission, not a command. The library may still run the algorithm sequentially, and on a machine with one CPU, like our runner, the results are the same but you gain no speed. Your part of the contract is that the callbacks must be safe to run concurrently: no data races, no locks that can block vectorized code, and (for par_unseq) no function calls that synchronize.

C++
#include <execution>
#include <algorithm>

std::sort(std::execution::par, v.begin(), v.end());

Try it

C++ Same results with different policies

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ Sorting and counting in parallel

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Syntax

C++
template<class ExecutionPolicy, class ForwardIt, class... Args>
/* result */ algorithm(ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, Args...);
Policy Meaning
std::execution::seq Run sequentially in the calling thread; element accesses are not interleaved.
std::execution::par May use several threads. Element accesses on one thread are not interleaved with each other.
std::execution::par_unseq May use several threads and vectorize, so one thread may interleave the callbacks of different elements.
std::execution::unseq C++20: vectorization allowed, but only on the calling thread.

Iterators: parallel overloads need at least forward iterators (not input iterators). Exceptions: if a callback throws out of a parallel algorithm, std::terminate is called, instead of the exception propagating. Allocation failure inside the library may still throw std::bad_alloc.

Usage notes

  • Parallelism pays off only for big inputs. Starting threads has a cost. Small ranges, or cheap work per element such as an addition, are often faster sequentially. Measure with realistic sizes.
  • No data races in callbacks. Writing to a shared variable (sum += x inside for_each) is undefined behavior. Use std::reduce or transform_reduce, or an atomic.
  • par_unseq forbids locking. A callback that takes a mutex, allocates, or throws may deadlock or terminate, since the thread may switch to another element while holding the lock. Use par if you need locks.
  • reduce is not accumulate. It may regroup and reorder operations, so the operation must be associative and commutative. Floating-point sums can differ slightly from a sequential loop.
  • accumulate has no policy overload. It is strictly left to right. Use std::reduce for the parallel version; std::inclusive_scan replaces partial_sum.
  • Library support differs. On Linux, libstdc++ implements policies through oneTBB when it is installed and otherwise runs them sequentially. MSVC and libc++ have their own back ends. Check what your toolchain gives you before expecting a speed-up.
  • Not every algorithm is parallel: std::ranges algorithms do not take execution policies in C++23.
  • Results must not depend on order. Output iterators in copy_if and remove_if still keep the input order; only the work is split.
  • Thread count is not under your control. The policy is a hint; there is no portable way to pick the number of threads.