it make me confused, i reading golang memory model, https://golang.org/ref/mem
var l sync.Mutex
var a string
func f() {
a = "hello, world"
l.Unlock()
}
func main() {
l.Lock()
go f()
l.Lock()
print(a)
}
Mutex Lock Unlock by atomic
UnLock: new := atomic.AddInt32(&m.state, -mutexLocked)
Lock: atomic.CompareAndSwapInt32(&m.state, 0, mutexLocked)
my question is, if atomic AddInt32, CompareAndSwapInt32 will cause memory barriers, if a will be visible in different goroutines.
In java, I know AtomicInteger, memory barriers by "volatile", keep thread field visible.