Originally Posted by
Toby Douglass
Yes.
This is quite close to the matter in my mind. For the compiler/processor, both of which see the world in terms of a single thread, it can seem entirely reasonable to place a value into a register and use it from there - for an indefinite period of time. However, in reality, where other threads are also writing to the value, the compiler and processor are unaware of changes being made elsewhere.
To ensure those changes are seen, we appropriately use memory barriers and atomic operations; but my concern is that once a variable has been copied into a register and is being used from there, that changes made to that variabe by other threads - even where we are correctly using memory barriers and atomic operations - will not propagate to the copy in the register.
To solve this, we must use volatile, since it prevents the compiler/processor from acting in this way. A value cannot be indefinitely stored in a register. It is the final link in the chain.