A read-modify-write operation reads a value, computes an update, then writes the result back. Though `++` and `+=` look like single actions, threads can interleave these steps: two may read the same old value and overwrite one another’s updates. The result is a race condition, and the final value may vary between runs. section 8.2 section 5.6
Two threads, one counter
If a counter holds 5, two threads can both read 5 and then both write 6. The counter ends at 6 instead of 7 because one increment is lost. An atomic operation makes the update indivisible; for a histogram, that can mean replacing `hist[bin]++` with `atomicAdd(&hist[bin], 1)`. section 8.2 section 8.10
Figure 8.1 The race condition (Scenario A, top) and the correct sequential execution (Scenario B, bottom). When T0 and T1 run concurrently (A), both threads read count=5 and both write 6, so one increment is lost: the final count is 6. When T0 completes before T1 starts (B), the final count is correctly 7.

Compare the interleaved and sequential counter updates to see how thread timing loses an increment. section 8.2




