Atomicity and Locks in Rust
Atomicity and Locks in Rust
Summary:
Introduction
The book "Rust Atomics and Locks" by Mara Bos is not just another guide to multithreading. It is a deep, technically rich exploration of how atomic operations, locks, and memory barriers actually work at the processor and operating system level, and how these mechanisms are represented in the Rust language. Published in 2023, the book immediately became a must-read for anyone writing high-load, safe, and efficient concurrent programs in Rust.
What this book is about
The book is structured as a gradual progression from simple to complex. It starts with the basics: what atomicity is, what types of atomic operations exist (load, store, CAS, fetch_add, etc.), and how they map to processor instructions. The author then moves on to the Rust memory model, explaining how the compiler and processor can reorder instructions and how this affects the correctness of multithreaded code. Special attention is given to memory barriers (memory ordering) — Acquire, Release, AcqRel, SeqCst — and their practical application. Next, classic synchronization primitives are examined: mutexes, condition variables, semaphores, barriers. Each primitive is not just described, but implemented from scratch using atomic operations. This allows the reader to understand the internal structure of locks and evaluate their cost. The book concludes with a discussion of lock-free data structures (stack, queue, hash table) and concurrent programming patterns such as read-copy-update (RCU) and hazard pointers.
Who this book is for
The book is intended for experienced developers already familiar with Rust syntax and having a basic understanding of multithreading. Beginners just starting to learn Rust will find it challenging: the author does not explain the basics of the language but immediately moves on to complex concepts. However, if you have already written multithreaded programs in C++, Java, or Go and want to understand how Rust makes concurrency safe and efficient — this book is for you.
What the reader will learn
- How atomic operations work at the CPU level (x86, ARM, RISC-V).
- What a memory model is and how it affects compiler optimizations.
- How to correctly choose memory ordering for different scenarios.
- How to implement a mutex, semaphore, and condition variable from scratch.
- How to write lock-free data structures without data races and UB.
- How to use
std::sync::atomicandstd::sync::Mutexin practice. - How to debug and test multithreaded code.
- How RCU and hazard pointers work in Rust.
Strengths
The first and main advantage of the book is depth. Mara Bos does not simply paraphrase the documentation but explains the physics of processes: why an atomic operation is expensive, how cache lines affect performance, what happens during a context switch. The second is practical orientation. Each chapter contains complete, working code examples that can be run and experimented with. The third is relevance. The book is written for the Rust 2021 edition and covers all modern language features, including std::sync::LazyLock and std::sync::OnceLock. The fourth is clarity of presentation. Despite the complexity of the topic, the author writes in a very structured manner, with clear definitions and explanations. The chapter on the memory model deserves special mention — it is the best I have encountered in Rust literature.
Who is recommended
The book will be useful for systems software developers, creators of databases, game engines, high-load web servers, and anyone who wants to write performant and safe concurrent code in Rust. If you work with embedded systems or write drivers — the book will provide an understanding of how atomic operations interact with hardware.