In order to be able to generate an assembly with the correct memory barriers, at compile time you need to know the C ++ order parameters.
The problem is the following statement:
memory_order order = memory_order_relaxed;
This is not a compile-time constant, so gcc is going to accept memory_order_seq_cst and insert the mfence :
Dump of assembler code for function bar1(): 0x0000000000400fc0 <+0>: movl $0x2a,0x20128a(%rip) # 0x602254 <g_x> 0x0000000000400fca <+10>: mfence 0x0000000000400fcd <+13>: mov 0x20127d(%rip),%eax # 0x602250 <g_y>
If you change it to:
constexpr memory_order order = memory_order_relaxed;
The relaxed parameter will take effect, and mfence will disappear:
Dump of assembler code for function bar1(): 0x0000000000400fc0 <+0>: movl $0x2a,0x201286(%rip) # 0x602250 <g_x> 0x0000000000400fca <+10>: mov 0x20127c(%rip),%eax # 0x60224c <g_y>
If you compile with -O3 optimization, running the binary now displays the reorder.
source share