Unsequenced Policy
Explore the C++20 unsequenced execution policy and its role in enabling vectorization through SIMD instructions in parallel algorithms. Understand why synchronization primitives can cause deadlocks and how to write safe, lock-free code for efficient parallel execution. Discover the differences between unsequenced, parallel, and parallel unsequenced policies as well as exception handling constraints in parallel algorithms.
We'll cover the following...
Understanding the unsequenced policy
The unsequenced policy was added in C++20. It tells the algorithm that the loop can be vectorized using, for example, SIMD instructions. In practice, this means that you cannot use any synchronization primitives in the code you pass to the algorithm since this could result in deadlocks.
To understand how a deadlock can occur, we will get back to the previous inadequate example when counting the total size of all strings in a vector. Assume that, instead of using std::reduce(), we protect the tot_size variable by adding a mutex, like this: ...