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-ordering of instructions from before to after and from after to before the memory barrier.
This is an example of a memory barrier in use:
instruction 1
instruction 2
instruction 3
mfence
instruction 4
instruction 5
instruction 6
Now my question is: Is the mfence instruction just a marker telling the CPU in what order to execute the instructions? Or is it an instruction that the CPU actually executes like it executes other instructions (for example: mov).
Every byte sequence that the CPU encounters amongst its code is an instruction that the CPU executes. There are no other kinds of instructions.
You can see this clearly in both the Intel instruction set reference and the specific page for mfence.
As you can see from the excerpt the
MFenceinstruction does quite a bit of work, rather than just being a marker of some sort.