Yes, on x86 and x86-64, as long as the value you are reading is correctly aligned. 32-bit int
s, they need to be aligned on a 4-byte border, so that access is atomic when reading, or that will almost always happen if you don't go out of your way to create an unbalanced int
(say, using a packed structure or doing custom arithmetic / pointer with byte buffers).
You probably also want to declare your variable as volatile
so that the compiler generates code that will repeatedly retrieve the variable from memory each time it is available. This will prevent optimizations such as register caching when it can be modified by another thread.
source share