当前位置:网站首页>Important knowledge of golang: sync Once explanation
Important knowledge of golang: sync Once explanation
2022-06-23 15:24:00 【yue_ xin_ tech】
sync.Once Introduce
As mentioned before Go Concurrent helper for :WaitGroup. alike , sync.Once It's also Go An official concurrency helper , It enables functional methods to execute only once , To achieve a similar init The effect of the function . Let's take a brief look at its usage :
func main() {
var once sync.Once
onceFunc := func() {
fmt.Println("Only once")
}
for i := 0; i < 10; i++ {
once.Do(onceFunc)
}
}
After execution here, we will only see once Only once Print information for , This is it. sync.Once One time effect of .
sync.Once Source code
Let's see sync.Once Source code :
type Once struct {
done uint32
m Mutex
}
func (o *Once) Do(f func()) {
// Atomic load identification value , Judge whether it has been executed
if atomic.LoadUint32(&o.done) == 0 {
o.doSlow(f)
}
}
func (o *Once) doSlow(f func()) { // I haven't executed a function yet
o.m.Lock()
defer o.m.Unlock()
if o.done == 0 { // Determine again whether the function has been executed
defer atomic.StoreUint32(&o.done, 1) // Atomic manipulation : Modify the identification value
f() // Execute function
}
}
It can be concluded from the above that ,sync.Once Is through the identification of a value , Atomic modification and loading , To reduce lock competition .
Interested friends can search the official account 「 Read new technology 」, Pay attention to more push articles .
If you can , Just like it by the way 、 Leave a message 、 Under the share , Thank you for your support !
Read new technology , Read more new knowledge .
边栏推荐
猜你喜欢

Moher College - manual SQL injection vulnerability test (MySQL database)
Explain in detail the principle and implementation of redis distributed lock
Redis缓存三大异常的处理方案梳理总结

An idea plug-in for automatically generating unit tests

How can genetic testing help patients fight disease?

2021-06-03

2021-06-07

百萬獎金等你來拿,首届中國元宇宙創新應用大賽聯合創業黑馬火熱招募中!
![[opencv450] salt and pepper noise demo](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[opencv450] salt and pepper noise demo

Idea view View the class file idea Class folder
随机推荐
Babbitt | metauniverse daily must read: meta, Microsoft and other technology giants set up the metauniverse Standards Forum. Huawei and Alibaba joined. NVIDIA executives said that they welcomed partic
labelme的JSON文件转成COCO数据集格式
Simple tutorial of live streaming with OBS
F5《2022年应用策略现状报告》:边缘部署及负载安全成亚太地区关注焦点
The "shoulder" of sales and service in the heavy truck industry, Linyi Guangshun deep ploughing product life cycle service
Analysis of graphical level-1 programming problem of Electronic Society: cat and mouse
mysql主从只同步部分库或表的思路与方法
Xampp中mysql无法启动问题的解决方法
直播间源码在开发前期必须做的工作及开发步骤
Un million de bonus vous attend, le premier concours d'innovation et d'application de la Chine Yuan cosmique Joint Venture Black Horse Hot Recruitment!
地平线开发板 调试
Slice() and slice() of JS
SQL注入漏洞(原理篇)
WebService interface publishing and calling
mysql 系列:总体架构概述
【云驻共创】智能供应链计划:提升供应链决策水平,帮助企业运营降本增效
F5 application strategy status report in 2022: edge deployment and load security become the focus of attention in the Asia Pacific Region
golang 重要知识:sync.Once 讲解
百万奖金等你来拿,首届中国元宇宙创新应用大赛联合创业黑马火热招募中!
【Pyside2】 pyside2的窗口在maya置顶(笔记)