My C line counter++; compiles to a single instruction on x86, add dword ptr [counter], 1, so I assumed it could not be interrupted halfway and was therefore safe to use from two threads. Yet with two threads each doing one million increments, the total comes out short on a multi-core PC.
On a Cortex-M4 the same line becomes three instructions: ldr, adds, str. When is a read-modify-write like this atomic, and what does the correct version look like in assembly on each architecture?




