Using the C++ memory model

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 ...

Get Embedded Programming with Modern C++ Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.