Beginning with the C++11 standard, C++ defined an API and primitives for threads and synchronization as part of the language. Memory synchronization in a system that has multiple processor cores is complicated because modern processors can optimize code execution by reordering instructions. Even when using atomic variables, there is no guarantee that the data is modified or accessed in the desired order, since the order can be changed by a compiler.
To avoid ambiguity, C++11 introduced the memory model, defining the behavior of the concurrent access to the memory region. As part of the memory model, C++ defined the std::memory_order enum, which gives hints to a compiler regarding the intended model of access. This helps the compiler to optimize the code in a way that does not interfere with the intended code behavior.
In this recipe, we will...