Based on what I have read, a CPU can re-order the execution of instructions, and a memory barrier prevents the re-ordering of instruction from before to after and from after to before the memory barrier.
But there is something that I am not sure of. Say I have the following instructions:
store x
store y
Let's say that the CPU decided to execute store y before store x.
How does the CPU does that, does it completely ignores store x and executes store y first? Or does the following happen?:
store xis executed, but it is not completed immediately (it becomes pending).store yis executed, and it is completed immediately.- The pending
store xis completed.
So basically, this gave the "illusion" that the instructions were executed out of order, even though they didn't, they only completed out of order.
I am asking this question to understand how a memory barrier work.
For example say I have the following instructions:
store x
mfence
store y
Now when the CPU executes these instructions, will the following happen?:
store xis executed, but it is not completed immediately (it becomes pending).mfenceis executed, now since this instruction is a memory barrier, the CPU will make sure that all pending operations before it (store x) will be completed before continuing with the execution of instructions.store yis executed.