Golang中Sync Mutex的锁定逻辑解析
Golang中Sync Mutex的锁定逻辑解析
func (m *Mutex) lockSlow() {
…
for {
…
if atomic.CompareAndSwapInt32(&m.state, old, new) {
…
if old&mutexStarving != 0 {
delta := int32(mutexLocked - 1<<mutexWaiterShift)
if !starving || old>>mutexWaiterShift == 1 {
delta -= mutexStarving
}
atomic.AddInt32(&m.state, delta)
break
}
awoke = true
iter = 0
}
}
}
我不完全理解 Go 源码中 sync.Mutex 的加锁逻辑。
如果一个 Goroutine 进入饥饿模式,并且此时 mutexWaiterShift == 1,那么饥饿模式会被移除。如果此时有一个新的 Goroutine 尝试获取锁,它能够成功吗?
更多关于Golang中Sync Mutex的锁定逻辑解析的实战教程也可以访问 https://www.itying.com/category-94-b0.html

