小编kin*_*001的帖子

为什么 atomic.StoreUint32 优于 sync.Once 中的正常分配?

在阅读Go的源代码时,我对src/sync/once.go中的代码有一个疑问:

func (o *Once) Do(f func()) {
    // Note: Here is an incorrect implementation of Do:
    //
    //  if atomic.CompareAndSwapUint32(&o.done, 0, 1) {
    //      f()
    //  }
    //
    // Do guarantees that when it returns, f has finished.
    // This implementation would not implement that guarantee:
    // given two simultaneous calls, the winner of the cas would
    // call f, and the second would return immediately, without
    // waiting for the first's call to f to complete.
    // This is …
Run Code Online (Sandbox Code Playgroud)

go

7
推荐指数
1
解决办法
282
查看次数

标签 统计

go ×1