Leanpub Header

Skip to main content

Lock-Free C++ in Production

Mastering the Memory Model with Hazard Pointers and Epoch-Based Reclamation

Lock-Free C++ in Production
This book is 95% completeLast updated on 2026-11-17

Your lock-free queue passed every test. It ran clean under ThreadSanitizer for a year. Then it shipped to ARM and started corrupting data, without a single crash. Lock-Free C++ in Production shows you why, and how to make sure it never happens to your code.

Free With Membership

With Membership

Free!

$39.00

You pay

Author earns

$

Also available for 2 book credits with a Reader Membership

PDF
About

About

About the Book

our lock-free queue passed every test. It ran clean under ThreadSanitizer for a year. Then it moved to ARM servers and started returning corrupted data, with no crash and no warning.

This book is about that gap between code that runs and code that is correct.

Most lock-free code works because of hardware it was never written for. x86 quietly adds orderings the code never asked for, and test machines rarely stall a thread at the worst possible instruction. This book shows you how to build lock-free C++ that is correct by proof rather than by luck, and how to verify it on every architecture you ship to.

What you’ll learn:

  • The C++ memory model, precisely. Every ordering is justified by the happens-before edge it creates, not by what one CPU happens to do.
  • Production data structures. SPSC, MPSC and MPMC queues, the Michael-Scott queue, Chase-Lev work-stealing, lock-free hash maps, skip lists, seqlocks, RCU and MCS locks, each with its progress guarantee stated.
  • Memory reclamation, solved. Hazard pointers, epoch-based reclamation, QSBR and interval-based schemes, built from the algorithm up to production domains, including C++26’s std::hazard_pointer and std::rcu.
  • Verification that finds real bugs. ThreadSanitizer’s blind spots, controlled scheduling, model checking with GenMC and Relacy, and linearizability checking.
  • Performance that holds up. False sharing, contention management, scalability measurement and NUMA placement.
  • Running it in production. Misuse-resistant APIs, debugging with core dumps and rr, and porting from x86 to ARM.

Every structure ships with complete, compilable modern C++, its stress test, and the deliberate bugs that prove the tests can catch it.

For C++ engineers who will run lock-free code in production, and who need it to be right.

Author

About the Author

Weston Ashgrove

Weston Ashgrove is a C++ systems engineer who builds software where being wrong or slow has a cost. He came up through quantitative trading, building order books, matching logic and the low-latency infrastructure underneath them and now brings the same discipline to performance engineering, concurrency, storage, networking, GPUs, and AI inference.

His rule for performance-critical code is simple: nothing is true until it has been profiled, fuzzed, and replayed. Every technique in his books comes with the measurement that proves it, the trade-off it costs, and the failure it prevents. He writes for working engineers who ship and debug production C++, and he leaves out anything that only works on a slide.

Contents

Table of Contents

Lock-Free C++ in Production

Mastering the Memory Model with Hazard Pointers and Epoch-Based Reclamation

Weston Ashgrove

Introduction

Part I: The Foundations

Chapter 1: Why lock-free, and when not to
  • 1.1 What locks really cost: contention, convoying, and priority inversion
  • 1.2 Progress guarantees: obstruction-free, lock-free, and wait-free
  • 1.3 When lock-free is the wrong answer
  • 1.4 Reference workloads and the concurrency test harness
Chapter 2: The hardware beneath atomics
  • 2.1 Cache coherence: MESI, MOESI, and the cost of sharing a line
  • 2.2 Store buffers, invalidation queues, and why reordering happens
  • 2.3 x86 total store order vs. ARM weak ordering
  • 2.4 The cost of atomic read-modify-write: LOCK prefixes and LL/SC

Part II: The Memory Model

Chapter 3: The C++ memory model, precisely
  • 3.1 Happens-before, synchronizes-with, and modification order
  • 3.2 Data races and why they are undefined behavior
  • 3.3 std::atomic, std::atomic_ref, and is_always_lock_free
  • 3.4 Coherence rules and what they guarantee even under relaxed ordering
Chapter 4: Memory orderings in practice
  • 4.1 Relaxed ordering: counters, statistics, and its real limits
  • 4.2 Acquire and release: safe publication patterns
  • 4.3 Sequential consistency: store-load ordering and when you truly need it
  • 4.4 Fences, signal fences, and why memory_order_consume failed
Chapter 5: Atomic operation patterns
  • 5.1 Compare-and-swap loops: weak vs. strong and spurious failure
  • 5.2 The ABA problem and tagged pointers
  • 5.3 Double-width CAS and 128-bit atomics
  • 5.4 Waiting without spinning: atomic::wait, notify, and futexes

Part III: Lock-Free Data Structures

Chapter 6: Queues
  • 6.1 Single-producer single-consumer ring buffers
  • 6.2 Multi-producer single-consumer queues
  • 6.3 Bounded multi-producer multi-consumer queues
  • 6.4 Unbounded queues: the Michael-Scott algorithm
  • 6.5 The Chase-Lev work-stealing deque
Chapter 7: Stacks, lists, and maps
  • 7.1 The Treiber stack and elimination backoff
  • 7.2 Lock-free linked lists with logical deletion
  • 7.3 Lock-free hash maps and split-ordered lists
  • 7.4 Lock-free skip lists
  • 7.5 Wait-free techniques: helping and fast-path/slow-path
Chapter 8: Synchronization primitives
  • 8.1 Seqlocks for read-mostly data
  • 8.2 Read-copy-update patterns in user space
  • 8.3 std::atomic<std::shared_ptr> and its hidden costs
  • 8.4 Spinlocks done right: TTAS, exponential backoff, and MCS locks

Part IV: Memory Reclamation

Chapter 9: The reclamation problem
  • 9.1 Why delete breaks lock-free code
  • 9.2 Reference counting and its limits under contention
  • 9.3 Split reference counts
  • 9.4 A taxonomy of reclamation schemes
Chapter 10: Hazard pointers
  • 10.1 The hazard pointer algorithm
  • 10.2 Building a hazard pointer domain in C++
  • 10.3 Retire lists, scanning, and amortized reclamation
  • 10.4 std::hazard_pointer in C++26
Chapter 11: Epoch-based reclamation
  • 11.1 The epoch algorithm
  • 11.2 Building an epoch-based reclaimer in C++
  • 11.3 The stalled-thread problem and bounding memory growth
  • 11.4 Quiescent-state reclamation and std::rcu in C++26
Chapter 12: Advanced reclamation
  • 12.1 Hybrid schemes: hazard eras and interval-based reclamation
  • 12.2 Comparing schemes by throughput, latency, and memory footprint
  • 12.3 Integrating reclamation with custom allocators
  • 12.4 Choosing a scheme for your workload

Part V: Correctness and Performance

Chapter 13: Proving it works
  • 13.1 ThreadSanitizer and its blind spots
  • 13.2 Stress testing and randomized scheduling
  • 13.3 Model checking with Relacy, CDSChecker, and GenMC
  • 13.4 Linearizability checking
Chapter 14: Making it fast
  • 14.1 False sharing and hardware_destructive_interference_size
  • 14.2 Contention management and backoff strategies
  • 14.3 Scalability benchmarking across core counts
  • 14.4 NUMA effects on lock-free structures

Part VI: Production

Chapter 15: Lock-free code in production
  • 15.1 Designing APIs around lock-free structures
  • 15.2 Debugging lock-free failures with core dumps and rr
  • 15.3 Portability between x86 and ARM
  • 15.4 The lock-free decision guide

Appendices

  • Appendix A: Memory ordering reference tables
  • Appendix B: Atomic instruction mapping for x86 and ARM
  • Appendix C: Reclamation scheme comparison
  • Appendix D: Lock-free code review checklist
  • Appendix E: Production libraries

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub