Try   HackMD

StoreLoad out-of-order experiment

Github repository

This program is modified from example source code in
https://preshing.com/20120515/memory-reordering-caught-in-the-act/.

This program is an experiment on memory order, where there should allow only one thread enter the

if(__atomic_load_n(&Y,__ATOMIC_RELAXED) != __atomic_load_n(&X,__ATOMIC_RELAXED))

statement.

However, according to the result of the experiment, if I don't apply the StoreLoad barrier, the r1 and r2 could both be one.

thread1 thread2
X=0 Y=0
synchronization by semaphor synchronizatoin by semaphor
store(&X,1) exchange(&Y,1)
if(X!=Y) if(X!=Y)

environment:

Architecture:                    x86_64
CPU op-mode(s):                  32-bit, 64-bit
Byte Order:                      Little Endian
Address sizes:                   39 bits physical, 48 bits virtual
CPU(s):                          8
On-line CPU(s) list:             0-7
Thread(s) per core:              2
Core(s) per socket:              4
Socket(s):                       1
NUMA node(s):                    1
Vendor ID:                       GenuineIntel
CPU family:                      6
Model:                           126
Model name:                      Intel(R) Core(TM) i5-1035G1 CPU @ 1.00GHz
Stepping:                        5
CPU MHz:                         1200.000
CPU max MHz:                     3600.0000
CPU min MHz:                     400.0000
BogoMIPS:                        2380.80
Virtualization:                  VT-x
L1d cache:                       192 KiB
L1i cache:                       128 KiB
L2 cache:                        2 MiB
L3 cache:                        6 MiB
NUMA node0 CPU(s):               0-7

with #define USE_CPU_FENCE 0

$ ./ordering 
1 reorders detected after 137 iterations
2 reorders detected after 11996 iterations
3 reorders detected after 33771 iterations
4 reorders detected after 39900 iterations
5 reorders detected after 40784 iterations
6 reorders detected after 40890 iterations
7 reorders detected after 40901 iterations
8 reorders detected after 55329 iterations
9 reorders detected after 65493 iterations
10 reorders detected after 106711 iterations
11 reorders detected after 119361 iterations
12 reorders detected after 119463 iterations
13 reorders detected after 133035 iterations
14 reorders detected after 146906 iterations
15 reorders detected after 148372 iterations