6. ConcurrencyRaces
Races
A race condition is a bug where the result depends on the exact timing of two or more threads, so the program works most of the time and fails unpredictably. The classic case is two threads adding one to a shared counter: the increment is really three steps, read, add, write back, and if both read before either writes, one update is lost. Because the bad timing may occur once in a million runs, these bugs are hard to reproduce and often vanish when you add logging. A data race is the narrower case of unsynchronized access to the same memory with at least one write, which C++ treats as undefined behavior. Locks, atomics, or not sharing mutable data prevent them.
In this lesson: Two threads’ instructions shown as cards. You pick the schedule and hunt for the lost-update interleaving.