美文网首页
解析Golang sync.Once源码

解析Golang sync.Once源码

作者: robertzhai | 来源:发表于2022-12-05 10:16 被阅读0次

数据结构

type Once struct {
    // done indicates whether the action has been performed.
    // It is first in the struct because it is used in the hot path.
    // The hot path is inlined at every call site.
    // Placing done first allows more compact instructions on some architectures (amd64/386),
    // and fewer instructions (to calculate offset) on other architectures.
    done uint32  // 初始值为0表示还未执行过,1表示已经执行过
    m    Mutex  // done 的lock
}

核心代码

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 why the slow path falls back to a mutex, and why
    // the atomic.StoreUint32 must be delayed until after f returns.

    if atomic.LoadUint32(&o.done) == 0 {  // 先检查是否没执行
        // Outlined slow-path to allow inlining of the fast-path.
        o.doSlow(f)  // 没执行就调用 doSlow
    }
}

func (o *Once) doSlow(f func()) {
    o.m.Lock()  // 加锁
    defer o.m.Unlock()
       // 采用双重检测机制 加锁判断done是否为零
    if o.done == 0 {  // 防止多个goroutine 并发进入doSlow 等待lock 后进入 再检查一次
        defer atomic.StoreUint32(&o.done, 1)  // 返回后设置已经执行
        f()  // 执行业务函数
    }
}

ref

相关文章

  • 解析Golang sync.Once源码

    数据结构 核心代码 ref https://segmentfault.com/a/1190000039294152...

  • golang sync.once解析

    实现原理(当前代码版本go version go1.11.4 ) 1.atomic 原子操作计数器,用于记录此On...

  • 深度剖析Golang sync.Once源码

    目录 什么是sync.Once 如何使用sync.Once 源码分析 什么是sync.Once Once 可以用来...

  • client-go的使用

    client-go和golang源码中的技巧 kubernetes client-go解析 解读 kubernet...

  • 深入源码分析golang之sync.Once

    什么是sync.Once 官方文档对它的描述是:一个对象将完全执行一次,不得复制。常常用来单例对象的初始化场景,或...

  • Centos7源码安装Golang

    源码安装Golang 下载Golang 源码wget https://studygolang.com/dl/gol...

  • golang sync.Once 应用

    sync.Once能确保实例化对象Do方法在多线程环境只运行一次,内部通过互斥锁实现 示例 输出 2018/03/...

  • golang的sync.Once

    很早就知道sync.Once,一直以来都觉得这个功能用处不大,所以就没关注,在项目中也没使用过它。原因很简单,可以...

  • Golang sync.Once使用

    程序输出如下,

  • ios三方库解析

    YYCache 源码解析 YTKNetwork 源码解析 MJRefresh 源码解析 VVeboTableVie...

网友评论

      本文标题:解析Golang sync.Once源码

      本文链接:https://www.haomeiwen.com/subject/tpgbfdtx.html