Example of C++ “Memory barrier” [duplicate]
This question already has answers here : Closed 6 years ago . C++ Memory Barriers for Atomics (2 answers) I was reading the answer to this question regarding the volatile keyword: https://stackoverflow.com/a/2485177/997112 The person says: The solution to preventing reordering is to use a memory barrier, which indicates both to the compiler and the CPU that no memory access may be reordered across this point. Placing such barriers around our volatile variable access ensures that even non-volatile accesses won't be reordered across the volatile one, allowing us to write thread-safe code.