Skip to content

Atomic Operations and the Memory Model ​

In the previous two chapters we discussed thread lifecycles and mutex-based synchronization — together they solved the fundamental problem of "how to make multiple threads cooperate safely." But a mutex carries an inherent cost: even when the critical section is nothing more than a simple increment of a single variable, you still have to go through the full lock → modify → unlock sequence. When performance requirements climb and critical sections shrink, we need lighter-weight tools.

In this chapter we enter the world of std::atomic and the C++ memory model. std::atomic taps the CPU's atomic instructions to guarantee that an operation is indivisible without taking a lock. Memory order, in turn, governs how the compiler and the CPU may reorder instructions, letting you make precise trade-offs between performance and predictability. Together the two form the theoretical foundation of lock-free programming — and the prerequisite for the lock-free data structures and atomic operation patterns discussed in later chapters.

In This Chapter ​

pdf-latest-4-g85128cc · 85128cc · 2026-10-05