Posted on 2006-11-05
From what I can tell, incrementing an integer variable is not atomic, and usually translates to 3 assembly instructions. (move variable into register, increment variable, write variable back to memory.)
So, if I wanted to keep a counter variable in a multi-threaded environment, to determine how many times some function was called, I assume that using a simple global variable which is incremented by the function is not guarenteed to work properly.
Am I correct in this assumption?
If so, is there anyway to write an atomic increment in C without using a mutex?