concurrencyoop
A thread-safe counter needs more than a shared integer
Anonymous
A read, increment, and write can interleave with another thread, so two updates may collapse into one. Use a lock around the whole read-modify-write operation, or use an atomic increment if the language and runtime provide one. The lock is straightforward; an atomic operation can reduce coordination overhead but still needs a clear memory-ordering contract.
0