你不需要另一个互斥锁,如果你只是在 int* 类型上操作,你可以一起抛弃互斥锁并使用 atomic.*
type shared struct {
sharedCounter int64 // member shared between multiple goroutines, protected by mutex
exclusiveCounter int64 // member exclusive of one goroutine -- is mutex needed?
}
func (s *shared) readCounter() int64 {
return atomic.LoadInt64(&s.sharedCounter)
}
func (s *shared) setCounter(i int64) {
atomic.StoreInt64(&s.sharedCounter, i)
}
playground
拉风的咖菲猫
相关分类