Namespaces
Variants
Views
Actions

std::execution::seq, std::execution::par, std::execution::par_unseq, std::execution::unseq

From cppreference.com
< cpp‎ | algorithm
 
 
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy, ranges::sort, ...
Execution policies (C++17)
execution::seqexecution::parexecution::par_unseqexecution::unseq
(C++17)(C++17)(C++17)(C++20)  
Non-modifying sequence operations
(C++11)(C++11)(C++11)
(C++17)
Modifying sequence operations
Partitioning operations
Sorting operations
(C++11)
Binary search operations
Set operations (on sorted ranges)
Heap operations
(C++11)
Minimum/maximum operations
(C++11)
(C++17)

Permutations
Numeric operations
Operations on uninitialized storage
(C++17)
(C++17)
(C++17)
C library
 
Defined in header <execution>
inline constexpr
std::execution::sequenced_policy seq { /* unspecified */ };
(since C++17)
inline constexpr
std::execution::parallel_policy par { /* unspecified */ };
(since C++17)
inline constexpr
std::execution::parallel_unsequenced_policy par_unseq { /* unspecified */ };
(since C++17)
inline constexpr
std::execution::unsequenced_policy unseq { /* unspecified */ };
(since C++20)

The execution policy types

have the following respective instances:

  • std::execution::seq,
  • std::execution::par,
  • std::execution::par_unseq, and
  • std::execution::unseq.

These instances are used to specify the execution policy of parallel algorithms, i.e., the kinds of parallelism allowed.

Additional execution policies may be provided by a standard library implementation (possible future additions may include std::parallel::cuda and std::parallel::opencl).

[edit] Example

#include <algorithm>
#include <chrono>
#include <cstdint>
#include <execution>
#include <iostream>
#include <random>
#include <vector>
 
void measure(auto policy, std::vector<std::uint64_t> v)
{
    const auto start = std::chrono::steady_clock::now();
    std::sort(policy, v.begin(), v.end());
    const auto finish = std::chrono::steady_clock::now();
    std::cout << std::chrono::duration_cast<std::chrono::milliseconds>(finish - start)
              << '\n';
};
 
int main()
{
    std::vector<std::uint64_t> v(700000);
    std::mt19937 gen{ std::random_device{}() };
    std::ranges::generate(v, gen);
 
    measure(std::execution::seq, v);
    measure(std::execution::unseq, v);
    measure(std::execution::par_unseq, v);
    measure(std::execution::par, v);
}

Output:

41ms
41ms
13ms
8ms

[edit] See also

execution policy types
(class) [edit]