Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

Algorithm execution-policy constants

The objects std::execution::seq, std::execution::par, std::execution::par_unseq and std::execution::unseq specify how an algorithm is permitted to execute. They do not guarantee a speedup; choosing a policy also changes the requirements on functions that process elements.

Version: C++23; repository compiler flag, production runtime not attested

Syntax

#include <algorithm>
#include <execution>

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

std::transform(policy, first, last, output, operation);

The <execution> header provides four inline constexpr objects, not functions or enumeration values. Pass one as the first argument to a policy-enabled algorithm overload, such as std::transform. Their respective types are sequenced_policy, parallel_policy, parallel_unsequenced_policy and unsequenced_policy in std::execution.

std::execution::seq requires sequential execution in the calling thread. std::execution::par permits multiple threads, but element-processing function calls within one thread do not overlap. std::execution::par_unseq permits both multiple threads and unsequenced calls within each thread, enabling vectorization. std::execution::unseq permits unsequenced calls in the calling thread without permitting additional threads. Policies grant permissions, not guarantees of parallelism, vectorization or speedup; sequential execution is allowed.

Do not depend on element-processing order or on another iteration running concurrently. Prevent data races. For unseq and par_unseq, calling a vectorization-unsafe standard-library function causes undefined behavior; avoid locking mutexes inside the element-processing function. For all four policies, an exception escaping such a function invokes std::terminate, even with seq. Failure to obtain temporary memory needed for parallelization can throw std::bad_alloc.

The example computes squares independently and prints results only after each algorithm finishes. Every policy produces the same expected result; this is not a performance measurement. This entry targets C++23; a repository compiler flag does not attest production-runtime support.

Examples

#include <algorithm>
#include <array>
#include <cstddef>
#include <execution>
#include <iostream>

int main() {
    const std::array<int, 4> input{1, 2, 3, 4};

    const auto run = [&](auto policy) {
        std::array<int, 4> output{};
        std::transform(policy, input.begin(), input.end(), output.begin(),
                       [](int value) noexcept { return value * value; });
        for (std::size_t i = 0; i < output.size(); ++i) {
            if (i != 0) std::cout << ' ';
            std::cout << output[i];
        }
        std::cout << '\n';
    };

    run(std::execution::seq);
    run(std::execution::par);
    run(std::execution::par_unseq);
    run(std::execution::unseq);
}

Result

1 4 9 16
1 4 9 16
1 4 9 16
1 4 9 16

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources