std::execut...llel_policy,(3)
๐ NAME ๐
Okay bestie, welcome to std::execut...llel_policy,(3) โ thatโs the page title โ from the C++ Standard Libary manual (yes, โLibaryโ is literally in the header, not me ๐ญ).
The NAME section is giving exact-and-slightly-chaotic: std::execution::sequenced_policy,std::execution::parallel_policy, - std::execution::sequenced_policy,std::execution::parallel_policy,. Commas included, periodt. โจ
๐ธ Synopsis ๐ธ
Defined in header <execution>:
class sequenced_policy { /* unspecified */ }; (1) (since C++17)
class parallel_policy { /* unspecified */ }; (2) (since C++17)
class parallel_unsequenced_policy { /* unspecified */ }; (3) (since C++17)
class unsequenced_policy { /* unspecified */ }; (4) (since C++20)
sequenced_policyโ this is the execution policy type used as a unique type to disambiguate parallel algorithm overloading, and it requires that a parallel algorithmโs execution may not be parallelized. The invocations of element access functions in parallel algorithms invoked with this policy (usually specified asstd::execution::seq) are indeterminately sequenced in the calling thread. No sneaky parallelism here, bestie.parallel_policyโ same unique-type-to-disambiguate-parallel-algorithm-overloading energy, but this one indicates that a parallel algorithmโs execution may be parallelized. The invocations of element access functions in parallel algorithms invoked with this policy (usually specified asstd::execution::par) are permitted to execute in either the invoking thread or in a thread implicitly created by the library to support parallel algorithm execution. Any such invocations executing in the same thread are indeterminately sequenced with respect to each other. Multithreading era ๐parallel_unsequenced_policyโ again, a unique type used to disambiguate parallel algorithm overloading, and it indicates that a parallel algorithmโs execution may be parallelized, vectorized, or migrated across threads (such as by a parent-stealing scheduler). The invocations of element access functions in parallel algorithms invoked with this policy are permitted to execute in an unordered fashion in unspecified threads, and unsequenced with respect to one another within each thread. Chaotic-good execution vibes.unsequenced_policyโ unique type, disambiguation duties, and it indicates that a parallel algorithmโs execution may be vectorized, e.g., executed on a single thread using instructions that operate on multiple data items. Vectorized and cute.
During the execution of a parallel algorithm with any of these execution policies, if the invocation of an element access function exits via an uncaught exception, std::terminate is called, but the implementations may define additional execution policies that handle exceptions differently.
โจ Notes โจ
When using parallel execution policy, it is the programmerโs responsibility to avoid data races and deadlocks:
int a[] = {0, 1};
std::vector<int> v;
std::for_each(std::execution::par, std::begin(a), std::end(a), [&](int i)
{
v.push_back(i * 2 + 1); // Error: data race
});
std::atomic<int> x {0};
int a[] = {1, 2};
std::for_each(std::execution::par, std::begin(a), std::end(a), [&](int)
{
x.fetch_add(1, std::memory_order_relaxed);
while (x.load(std::memory_order_relaxed) == 1) { } // Error: assumes execution order
});
int x = 0;
std::mutex m;
int a[] = {1, 2};
std::for_each(std::execution::par, std::begin(a), std::end(a), [&](int)
{
std::lock_guard<std::mutex> guard(m);
++x; // correct
});
Bestie, those comments are not decorative.// Error: data raceand// Error: assumes execution orderare literally the red flags. Do not collect them like Pokรฉmon. ๐
Unsequenced execution policies are the only case where function calls are unsequenced with respect to each other, meaning they can be interleaved. In all other situations in C++, they are indeterminately-sequenced (cannot interleave). Because of that, users are not allowed to allocate or deallocate memory, acquire mutexes, use non-lockfree std::atomic specializations, or, in general, perform any vectorization-unsafe operations when using these policies (vectorization-unsafe functions are the ones that synchronize-with another function, e.g. std::mutex::unlock synchronizes-with the next std::mutex::lock).
int x = 0;
std::mutex m;
int a[] = {1, 2};
std::for_each(std::execution::par_unseq, std::begin(a), std::end(a), [&](int)
{
std::lock_guard<std::mutex> guard(m); // Error: lock_guard constructor calls m.lock()
++x;
});
Thatstd::lock_guardmoment is a no-no withpar_unseq. The comment says it all:// Error: lock_guard constructor calls m.lock().
If the implementation cannot parallelize or vectorize (e.g. due to lack of resources), all standard execution policies can fall back to sequential execution.
๐ See also ๐
These are the global execution policy objects (constant) that go with this page:
seqโ global execution policy object (constant) (C++17)parโ global execution policy object (constant) (C++17)par_unseqโ global execution policy object (constant) (C++17)unseqโ global execution policy object (constant) (C++20)
Sign-off time, bestie: this page came from http://cppreference.com, dated 2024.06.10 โ go forth and parallelize responsibly, mwah ๐โจ๐
๐ฆ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 ยท MIT
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms