std::execution::unsequenced_policy
This policy specifies that the execution of the algorithm may be vectorized, i.e, executed on a single thread using instructions that operate on multiple data items.
Syntax of unsequenced_policy
stlFunction (std::execution::unseq, ...other_arguments...);
Example of unsequenced_policy
C++
#include <algorithm> #include <iostream> #include <vector> #include <execution> int main() { std::vector< int > v = { 1, 2, 3, 4, 5 }; std::for_each(std::execution::unseq, v.begin(), v.end(), []( int x) { std::cout << x << " " ; }); return 0; } |
Output
1 2 3 4 5
Advantages of unsequenced_policy
- Fast Execution on a single thread
- Avoids Race Conditions
Disadvantages of unsequenced_policy
- Some Hardware may not support vectorization.
- Non-Deterministic execution sequence.
Execution Policy of STL Algorithms in Modern C++
C++ algorithms are a set of pre-defined functions that can perform various operations on containers, such as arrays, vectors, and lists. These algorithms have a defined execution policy that determines how they execute and how they interact with the underlying hardware.
The C++ 17 standard introduces three new execution policies and one policy was introduced in C++20. These execution policies in C++ allow algorithms to be executed in different ways depending on the requirements of the task and the hardware available. They are as follows:
- std::execution::sequenced_policy
- std::execution::parallel_policy
- std::execution::parallel_unsequenced_policy
- std::execution::unsequenced_policy