Phase 2 of 4 · Study weeks 5-8
Concurrency and the memory model
This phase makes the second checkpoint possible: two threads that pass messages with no race, no loss and no reordering.
Topics every week
- Week 58 h
- Threads, mutexes and condition variables
- Data races
- Atomics and sequentially consistent ordering
- ThreadSanitizer
Hand inA mutex queue between two threads, and a seeded race caught by TSan
- Week 68 h
- Acquire and release ordering
- Relaxed atomics and where they are safe
- The single-producer single-consumer ring buffer
- Cache-line padding
Hand inA lock-free SPSC queue with a written ordering argument for each operation
- Week 78 h
- Weak memory models and what x86 hides
- The ABA problem
- Verification: stress tests, TSan and a model checker
- Hazard pointers and RCU (read the status)
Hand inA weak-memory run or a model-checker run of the queue, with the result explained
- Week 88 h
- Thread pools and executors
- Coroutines: costs and pitfalls
std::execution(check the status in your toolchain)- Shutdown and cancellation
Hand inCheckpoint 2, with the 100-million-message run
Practice projects
- Practice 1, the seeded race (Week 5): share a plain counter between two threads. Done when: TSan reports the race, the fix passes 1,000 runs and you can say why the unfixed program usually still "works" on x86.
- Practice 2, the ordering argument (Week 6): write the SPSC queue with acquire and release. Done when: each atomic operation has a one-line reason, and weakening any one to relaxed makes a stress test or the model checker fail.
- Practice 3, the stress test (Week 7): move 100 million sequence-numbered messages. Done when: none is lost, duplicated or out of order, over 10 runs and with the producer paused at random.
- Optional, a clean shutdown: stop both threads under load. Done when: no message is dropped after the stop signal and TSan stays quiet.
GitHub repos to study
Existence and last commit were checked on 22 Nov 2026 by shallow clone. Every repository below had a commit in the last six months.
| Repo | What to read | Week |
|---|---|---|
| llvm/llvm-project | The ThreadSanitizer runtime in compiler-rt | 5 |
| microsoft/STL | The implementation of atomic, mutex and jthread | 6 |
| chriskohlhoff/asio | Executors and the proactor model | 8 |
| axboe/liburing | Submission and completion rings: another SPSC pattern | 6 |
Milestone checkpoint
Build Checkpoint 2. Start with the mutex queue and the seeded race, then replace the queue with the SPSC ring and write the ordering argument. Run TSan 1,000 times, the 100-million-message stress test and the weak-memory or model-checker run. Hand in the ordering argument, the stress results, ADR-3 (mutex or lock-free, with the measured difference) and ADR-4 (shutdown protocol).
Check yourself
Answer each question in your head first, then open it to check.
Why does a data race on a plain
intcount as undefined behaviour even if it looks harmless?The standard gives a racy program no meaning, so the compiler may cache the value in a register or reorder accesses. A sanitizer or a different compiler can then show a different result.What does release on a store and acquire on a load guarantee?
Everything the writer did before the release is visible to a reader that sees the stored value through the acquire.Why can a program pass on x86 and fail on ARM?
x86 keeps stores in order, so some missing ordering never shows. ARM may reorder them, so the same code can read stale data.What is false sharing, and how do you detect it?
Two threads write different variables on the same cache line, so the line bounces between cores. Look for a high rate of cache misses on a variable nobody shares, and confirm with padding.Why is a lock-free queue not always faster than a mutex queue?
Under low contention a mutex is one atomic operation. A lock-free queue adds care and complexity and gains only when contention or latency spikes from blocking matter.