As far as I have understood, mfence
is a hardware memory barrier while asm volatile (\"\" : : : \"memory\")
is a compiler barrier. But,can asm vo
There are two reorderings, one is compiler reordering, the other one is CPU reordering.
x86/x64 has a relatively strong memory model, but on x86/x64 StoreLoad reordering (later loads passing earlier stores) CAN happen. see http://en.wikipedia.org/wiki/Memory_ordering
asm volatile ("" ::: "memory")
is just a compiler barrier.asm volatile ("mfence" ::: "memory")
is both a compiler barrier and CPU barrier.that means, only use a compiler barrier, you can only prevent compiler reordering, but you can not prevent CPU reordering. that means there is no reordering when compiling source code, but reordering can happen in runtime.
So, it depends your needs, which one to use.