A duplicate was directed here, and this needs an update. The “new” C11 language enables an atomic attribute which admits that:
_Atomic int a;
...
a += 3
can be compiled into an (atomic) unbounded loop. Thanks for the gift standards folks, I really wish you hadn’t.
1: in some architectures, atomic operations are only possible on memory which supports certain access protocols. ARMv7, MIPS for example turn the sequence into:
do {
x = LoadLinked(a) + 3;
} while !StoreConditional(x, &a);
but LoadLinked/StoreConditional is undefined for some memory/cache types. Enjoy debugging that.
2: Related is false sharing which is an artifact of LoadLinked, StoreConditional operating upon cache lines (eg. 32, 64, 256 bytes) not sub-blocks. So:
_Atomic int a[4];
might require 4* cache line size (thus 1024 bytes) to safely permit simultaneous atomic operations on a[n] and a[n+1], because 4 cpu’s could be fighing to update a[0..3], but never succeeding.
Hopefully the next standard will recognize the inherent failure of attribute decoration, and reinstate c89 as the rightful C standard.