Given a c++ code snip:
int a = 0;
atomic<int> b{0};
Thread 1
a = 1;
b.store(1,memory_order_release);
Thread 2
while(!b.load(memory_order_acquire));
assert(a==1);
We know the assert never fire.
At the other hand, golang atomic.Store uses xchg instruction which implicts memory-barrier, so it can result in memory_order_release semantics as c++11.
//go:noescape
func Store(ptr *uint32, val uint32)
TEXT runtime∕internal∕atomic·Store(SB), NOSPLIT, $0-12
MOVQ ptr+0(FP), BX
MOVL val+8(FP), AX
XCHGL AX, 0(BX)
RET
However, the implementation of atomic.Load is pure go code, which means just mov instruction when assembly.
//go:nosplit
//go:noinline
func Load(ptr *uint32) uint32 {
return *ptr
}
So, does golang atomic.Load have a acquire semantics?
If do how it works, and if not how to insure memory-ordering or make a=1 visible?