In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail the example analysis of sync.Once in golang. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
Sync.once can control that the function can only be called once and cannot be called repeatedly.
We can implement a thread-safe singleton pattern with the following code
Package singleton
Import (
"fmt"
"sync"
)
Type object struct {
Name string
}
Var once sync.Once
Var obj * object / / Singleton pointer
/ / expose method outsourcing calls
Func Instance () * object {
Once.Do (getObj)
Return obj
}
Func getObj () {
If obj = = nil {
Obj = new (object)
/ / other initialization events can be done
}
}
/ / singleton test
Func (obj * object) Test () {
Fmt.Println (obj.name)
}
What if we want to implement such a function ourselves?
Define a status variable to describe whether it has been executed
Use sync.Mutex or sync.Atomic to obtain the status state safely and determine whether to execute a specific function according to the state.
Then take a look at how sync.Once is actually implemented.
/ / Once is an object that will perform exactly one action.
Type Once struct {
M Mutex
Done uint32
}
/ / double-layer checking mechanism is used
Func (o * Once) Do (f func ()) {
If atomic.LoadUint32 (& o.done) = = 1 {
Return
}
/ / Slow-path.
O.m.Lock ()
Defer o.m.Unlock ()
/ / it needs to be rejudged here, because it is possible for other gotoutine to change the state value of status between the state value taken out by atomic.LoadUint32 and o.m.Lock ().
If o.done = = 0 {
F ()
Atomic.StoreUint32 (& o.done, 1)
}
}
Some netizens have written more concise code, do not know why the official did not adopt the following implementation.
Type Once struct {
Done int32
}
Func (o * Once) Do (f func ()) {
If atomic.LoadInt32 (& o.done) = = 1 {
Return
}
/ / Slow-path.
If atomic.CompareAndSwapInt32 (& o.done, 0,1) {
F ()
}
} this is the end of the article on "sample Analysis of sync.Once in golang". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.