doutao5499 2018-10-10 10:50
浏览 232
已采纳

使用CompareAndSwapInt32的sync.Once.Do()

Go implements the sync.Once as such:

type Once struct {
    m    Mutex
    done uint32
}

func (o *Once) Do(f func()) {
    if atomic.LoadUint32(&o.done) == 1 {
        return
    }
    // Slow-path.
    o.m.Lock()
    defer o.m.Unlock()
    if o.done == 0 {
        defer atomic.StoreUint32(&o.done, 1)
        f()
    }
}

I'm trying to understand the need for the mutex here, what would be the issue with implementing it as this ?

func (o *Once) Do(f func()) {
  if atomic.CompareAndSwapUInt32(&o.done, 0, 1) {
    f()
  }
}
  • 写回答

1条回答 默认 最新

  • douzhan5058 2018-10-10 11:01
    关注

    Removing the mutex breaks one of the documented behaviours:

    no call to Do returns until the one call to f returns

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?