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.
However, memory barriers also ensure that all pending reads/writes are executed when the barrier is reached, so it effectively gives us everything we need by itself, making volatile unnecessary. We can just remove the volatile qualifier entirely.
How is this "memory barrier" implemented in C++?
EDIT:
Could someone give a simple code example please?