Demystifying std::memory_order
Atomic variables are an important tool for low-latency and real-time audio processing as they are essential to implementing any kind of lock-free algorithm or data structure. In C++, atomic variables are readily available in the Standard Library through std::atomic. However, in order to use std::atomic not only correctly, but also efficiently and performantly, it is crucial to pick the correct memory order for the atomic operation at hand depending on the requirements of the algorithm. This can be accomplished via the std::memory_order parameter that every atomic operation in C++ accepts. Yet memory order in C++ is notoriously hard to reason about and often misunderstood.
In this talk, we give a brief overview over the C++ memory model, why it is needed, and how it maps to what happens in actual hardware. We explain the differences between relaxed, consume, acquire, release, and sequentially consistent memory order, and give practical guidelines for which to use when for working with atomic reads, writes, read-modify-write operations, and memory fences.
Timur Doumler
Timur Doumler is a software engineer specialising in low-latency and real-time C++. He works at Citadel Securities and is an active member of the ISO C++ standard committee, where he has (co-)authored many successful proposals including [[assume]], std::inplace_vector, and contract assertions, and is currently serving as the chair of SG22, the C and C++ Liaison Group.
Timur began his C++ journey in computational astrophysics, working on large-scale cosmological simulations. After completing a PhD in astrophysics, he spent a decade in the audio and music technology industry and co-founded the music tech startup Cradle. He has also worked at JetBrains, contributing to their in-house C++ frontend and later serving as Developer Advocate for the CLion IDE and other popular C++ tools.
Timur is passionate about correct code, effective tools, and the evolution of the C++ language.