I am trying to understand what is a memory barrier exactly.
Based on what I know so far, a memory barrier (for example: mfence
) is used to prevent the re-orderi
Your question has the wrong assumptions. The MFENCE does not prevent the reordering of instructions (see highlighted quote). For example if there is a stream of 1000 instructions that only operate on registers and a MFENCE instruction is placed in the middle then it will have no effect on how the CPU reorders those instructions.
The MFENCE instruction is ordered with respect to all load and store instructions, other MFENCE instructions, any LFENCE and SFENCE instructions, and any serializing instructions (such as the CPUID instruction). MFENCE does not serialize the instruction stream.
Instead, the MFENCE instruction prevents the reordering of loads and stores to the cache and main memory.