Data Races and Atomicity of Compound Operations
Distinguish unordered conflicting accesses from lost updates within a sequence made of individually atomic operations.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 2
- Reading time
- 1 min
In this article2 chapters
A data race depends on conflict and order
In the C++ memory model, two potentially concurrent conflicting actions with at least one non-atomic action and neither happening before the other can form a data race. A write and an ordinary read of the same scalar are a standard conflict. Use an appropriate synchronization discipline to order them. Reading and writing separate distinct locations need different reasoning, including what the application invariant relates.
Atomic loads and stores do not combine automatically
Two threads can each atomically read counter value \(10\), compute \(11\), and atomically store \(11\). The final value is eleven, even though two increments were intended. An atomic read–modify–write operation or lock-protected critical section can represent the whole increment instead. Happens-before defines relevant orders. Compare-and-swap atomically conditions an update on the value just observed.[1][2]