Execution Policies

This lessons discusses the three types of execution policies as well as the differences between them.

Execution Policy Parameter

The execution policy parameter will tell the algorithm how it should be executed. We have the following options:

Policy Name Description
sequenced_policy It is an execution policy type used as a unique type to disambiguate parallel algorithm overloading and requires that a parallel algorithm’s execution not be parallelised.
parallel_policy It is an execution policy type used as a unique type to disambiguate parallel algorithm overloading and indicate that a parallel algorithm’s execution may be parallelised.
parallel_unsequenced_policy It is an execution policy type used as a unique type to disambiguate parallel algorithm overloading and indicate that a parallel algorithm’s execution may be parallelised and vectorised.

We have also three global objects corresponding to each execution policy type:

  • std::execution::seq
  • std::execution::par
  • std::execution::par_unseq

Please note that execution policies are unique types, with their corresponding global objects. They are not enumerations, nor do they share the same base type.

Execution policy declarations and global objects are located in the <execution> header.

Understanding Execution Policies

To understand the difference between execution policies, let’s try to build a model of how an algorithm might work.

Consider a simple vector of float values. In the below example, each element of the vector is multiplied by 2 and then the result is stored into an output container:

Get hands-on with 1400+ tech skills courses.