cpp-concurrency-atomicslisted
Install: claude install-skill rustcircle/c101
# C++ concurrency, atomics and the memory model
Ground rule: two conflicting accesses (same memory, at least one write) from different threads
without a happens-before edge are a **data race, which is UB**, not "a stale read". Every shared
mutable object is either protected by a mutex, atomic, or handed over through a synchronising
operation (queue, `join`, promise). `volatile` is not synchronisation.
Library gaps to check first: `std::atomic<std::shared_ptr>` exists in libstdc++ 12+ and MSVC
19.27+ but not in libc++ (Apple clang included); `std::jthread` is complete in libc++ 20;
C++26 `<hazard_pointer>`, `<rcu>` and `std::execution` have no shipping standard-library
implementation listed as of October 2026 (use folly, liburcu and NVIDIA stdexec).
Deep material: [memory-model patterns: SPSC ring, seqlock, Dekker, fences, lock hierarchy](references/memory-model-patterns.md) |
[coroutines and std::execution](references/coroutines-execution.md)
## 1. Choosing the tool
| Need | Tool | Notes |
|---|---|---|
| protect compound state | `std::mutex` + `std::scoped_lock` (`std::unique_lock` when you must unlock early or wait) | default choice; uncontended lock/unlock is a few atomic ops |
| many readers, rare writers, long reads | `std::shared_mutex` + `std::shared_lock` | measure: for short sections it is often slower than `std::mutex` |
| several mutexes at once | `std::scoped_lock lk(a, b);` | deadlock-avoidance algorithm, any argument order |
| wait for a condition | `std::c