Global variables and threads
问题 If I have a global counter and I have two threads that are each incrementing it 100 times in a loop, why is it possible that I can have a value other than 200? I don't see how accessing the variable is nonatomic. 回答1: That is because for most environments, incrementing a memory location is not an atomic operation. The sequence of events would be something like The compiler places the value 0 at the memory address 0xABCD Thread 1 reads 0xABCD into a register and increments the register value