这让我很困惑,我正在阅读 golang 内存模型,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)
}
Run Code Online (Sandbox Code Playgroud)
互斥锁通过原子解锁
UnLock: new := atomic.AddInt32(&m.state, -mutexLocked)
Lock: atomic.CompareAndSwapInt32(&m.state, 0, mutexLocked)
Run Code Online (Sandbox Code Playgroud)
我的问题是,原子 AddInt32、CompareAndSwapInt32 是否会导致内存障碍,如果a在不同的 goroutine 中可见的话。
在java中,我知道AtomicInteger,内存屏障通过“易失性”,保持线程字段可见。