首页 > 其他分享 >No.0: sync.Once

No.0: sync.Once

时间:2022-12-16 18:01:02浏览次数:24  
标签:Do sync done func No.0 call first Once

go version 1.18.9

// Once is an object that will perform exactly one action.
//
// A Once must not be copied after first use. 因为使用之后内部状态已经改变,但是就算copy使用也不会导致panic.
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
	m    Mutex
}

// Do calls the function f if and only if Do is being called for the
// first time for this instance of Once. In other words, given
// 	var once Once
// if once.Do(f) is called multiple times, only the first call will invoke f,
// even if f has a different value in each invocation. A new instance of
// Once is required for each function to execute.
//
// Do is intended for initialization that must be run exactly once. Since f
// is niladic, it may be necessary to use a function literal to capture the
// arguments to a function to be invoked by Do:
// 	config.once.Do(func() { config.init(filename) })
//
// Because no call to Do returns until the one call to f returns, if f causes
// Do to be called, it will deadlock.
//
// If f panics, Do considers it to have returned; future calls of Do return
// without calling f.
//
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)
	}
}

func (o *Once) doSlow(f func()) {
	o.m.Lock()
	defer o.m.Unlock()
	if o.done == 0 {
		defer atomic.StoreUint32(&o.done, 1)
		f()
	}
}

eg:

package main

import (
	"fmt"
	"sync"
)

func main() {
	var a = sync.Once{}
	a.Do(func() {
		fmt.Println(1)
	})

	b := a

	b.Do(func() {
		fmt.Println(2)
	})
}
程序输出: 1

标签:Do,sync,done,func,No.0,call,first,Once
From: https://www.cnblogs.com/cs4liuyue/p/16988006.html

相关文章

  • 使用Rsync在 Linux 上传输文件的示例
    在Linux操作系统上,“rsync”代表远程同步同步。它是用于将文件和目录从源(SRC)同步(复制)到目标(DEST)的实用程序。文件和目录可以在本地主机上同步,也可以在远程主机上......
  • 5分钟带你彻底搞懂async底层实现原理!
    ES2017标准引入了async函数,使得异步操作变得更加方便。async函数是什么?一句话,它就是Generator函数的语法糖。研究async的原理,就必须先弄清楚Generator是个啥。......
  • python并发编程之asyncio协程(三)
    协程实现了在单线程下的并发,每一个协程共享线程的几乎全部的资源,除了协程本身私有的上下文栈;协程的切换属于程序级别的切换,对于操做系统来讲是无感知的,所以切换速度更快、......
  • 秒懂 Golang 中的 条件变量(sync.Cond)
    本篇文章面向的读者:已经基本掌握Go中的协程(goroutine),通道(channel),互斥锁(sync.Mutex),读写锁(sync.RWMutex)这些知识。如果对这些还不太懂,可以先回去把这几个知识点解......
  • 【Linux】Linux 同步远程文件 或 本地文件 rsync命令
    目录​​介绍​​​​特征​​​​用法​​​​远程拷贝同步文件、目录​​​​本地到远程​​​​远程到本地​​​​本地拷贝同步文件、目录​​​​同步一个文件从本地一......
  • 对于async和await的使用同步、异步、顺序执行方式
    结论同步还是异步,区别如下:同步:你使用 await 修饰符去调用一个异步(async)方法(是异步方法,不过是阻塞式的,可简单理解为同步);异步:你获取异步方法返回的 Task,就是异步(后文......
  • 对于async和await的使用方式、作用效果不怎么理解 ?没关系,初步看这篇就够了
    结论同步还是异步,区别如下:同步:你使用 await 修饰符去调用一个异步(async)方法(是异步方法,不过是阻塞式的,可简单理解为同步);异步:你获取异步方法返回的 Task,就是异步(后文......
  • 对于async和await的使用方式、作用效果不怎么理解 ?没关系,初步看这篇就够了。
    结论同步还是异步,区别如下:同步:你使用 await 修饰符去调用一个异步(async)方法(是异步方法,不过是阻塞式的,可简单理解为同步);异步:你获取异步方法返回的 Task,就是异步(后文......
  • 异步编程 async与await
    原文链接:http://t.zoukankan.com/itjeff-p-4424810.html原文链接:https://learn.microsoft.com/zh-cn/dotnet/csharp/whats-new/csharp-version-historyC#5.0版C#版本......
  • @Async实现异步任务
    1、@Async是SpringBoot自带的一个执行步任务注解@EnableAsync//开启异步@SpringBootApplicationpublicclassApplication{publicstaticvoidmain(String[]a......