Warnings on Memory Models and Atomics

2022/05/28

Against better judgement, I’ve gone down the rabbit hole of learning about memory models of various languages and systems. Before proceeding to the main content, it’s customary among such articles to notify the reader that coming anywhere close to memory models can be too much fun ill-advised. Below are some examples I’ve collected. Please be warned :)

Herb Sutter, atomic Weapons: The C++ Memory Model and Modern Hardware:

It’s a session that includes topics I’ve publicly said for years is Stuff You Shouldn’t Need To Know and I Just Won’t Teach, but it’s becoming achingly clear that people do need to know about it. Achingly, heartbreakingly clear, because some hardware incents you to pull out the big guns to achieve top performance, and C++ programmers just are so addicted to full performance that they’ll reach for the big red levers with the flashing warning lights. Since we can’t keep people from pulling the big red levers, we’d better document the A to Z of what the levers actually do, so that people don’t SCRAM unless they really, really, really meant to.

Fedor Pikus, C++ atomics, from basic to advanced. What do they really do?:

Welcome to the Defence Against the Dark Arts class, where you will learn about the more radioactive parts of C++.

Paolo Bonzini, An introduction to lockless algorithms:

Low-level knowledge of the memory model is universally recognized as advanced material that can scare even the most seasoned kernel hackers; our editor wrote (in the July article) that “it takes a special kind of mind to really understand the memory model”. It’s been said that the Linux kernel memory model … can be used to frighten small children, and the same is probably true of just the words “acquire” and “release”.

The Go Memory Model:

If you must read the rest of this document to understand the behavior of your program, you are being too clever.

Don’t be clever.

The Rustonomicon, Chapter 8.3, Atomics:

… the memory model for atomics from C++20 … is quite complex and known to have several flaws … Trying to fully explain the model in this book is fairly hopeless. It’s defined in terms of madness-inducing causality graphs that require a full book to properly understand in a practical way.