Unsequenced Policy

Learn about the unsequenced policy and how it operates, as well as the parallel unsequenced policy and handling exceptions in the process.

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:

Get hands-on with 1200+ tech skills courses.