std::execut...ution::unseq(3)
π NAME π
okay so this is std::execut...ution::unseq(3), straight out of the C++ Standard Libary manual β yes bestie, βLibary,β thatβs the spelling in the header, we move π
.
The whole crew string is exactly std::execution::seq,std::execution::par,std::execution::par_unseq,std::execution::unseq.
And because this page is extra, it says the name again: std::execution::seq,std::execution::par,std::execution::par_unseq,std::execution::unseq. Periodt. π
β¨ Synopsis β¨
Defined in header <execution>.
The syntax tea β:
inline constexpr
(since C++17)
std::execution::sequenced_policy seq { /* unspecified */ };
inline constexpr
(since C++17)
std::execution::parallel_policy par { /* unspecified */ };
inline constexpr
std::execution::parallel_unsequenced_policy par_unseq { /*
(since C++17)
unspecified */ };
inline constexpr
(since C++20)
std::execution::unsequenced_policy unseq { /* unspecified */ };
So the execution policy types are:
std::execution::sequenced_policystd::execution::parallel_policystd::execution::parallel_unsequenced_policystd::execution::unsequenced_policy
and their respective instances are literally:
std::execution::seqstd::execution::parstd::execution::par_unseqstd::execution::unseq
These instances are used to specify the execution policy of parallel algorithms β i.e., the kinds of parallelism allowed. No cap, thatβs the whole vibe: youβre telling the algorithm what flavor of parallel energy itβs allowed to have. π
Additional execution policies may be provided by a standard library implementation (possible future additions may include std::parallel::cuda and std::parallel::opencl). π
πΈ Example πΈ
// Run this code β yes bestie, run it:
#include <algorithm>
#include <chrono>
#include <cstdint>
#include <iostream>
#include <random>
#include <vector>
#ifdef PARALLEL
#include <execution>
namespace execution = std::execution;
#else
enum class execution { seq, unseq, par_unseq, par };
#endif
void measure([[maybe_unused]] auto policy, std::vector<std::uint64_t>
v)
{
const auto start = std::chrono::steady_clock::now();
#ifdef PARALLEL
std::sort(policy, v.begin(), v.end());
#else
std::sort(v.begin(), v.end());
#endif
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(1'000'000);
std::mt19937 gen {std::random_device{}()};
std::ranges::generate(v, gen);
measure(execution::seq, v);
measure(execution::unseq, v);
measure(execution::par_unseq, v);
measure(execution::par, v);
}
π» Possible output: π»
bestie, it might spit out something like this:
// online GNU/gcc compiler (PARALLEL macro is not defined)
81ms
80ms
79ms
78ms
// with g++ -std=c++23 -O3 ./test.cpp -ltbb -DPARALLEL
165ms
163ms
30ms
27ms
π See also π
sequenced_policyβ (C++17)parallel_policyβ (C++17)parallel_unsequenced_policyβ (C++17)unsequenced_policyβ (C++20)- execution policy types β (class)
source/version: cppreference.com, 2024.06.10 β shoutout to the cppreference girlies for keeping us fed, 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