In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article is to share with you about golang value concurrency security is another way to play, the editor feels very practical, so share with you to learn, I hope you can learn something after reading this article, say no more, follow the editor to have a look.
Another way to play with golang value concurrency security is to use atomic.Value and look at a piece of code.
Package mainimport ("sync"sync/atomic"time") func main () {var m atomic.Value type Map map [string] string m.Store (make (Map)) var mu sync.Mutex read: = func (key string) (val string) {M1: = m.Load (). (Map) return M1 [key]} insert: = func (key Val string) {mu.Lock () defer mu.Unlock () M1: = m.Load (). (Map) m2: = make (Map) for k V: = range M1 {m2 [k] = v} m2 [key] = val m.Store (m2)} go func () {for {insert ("k") "v") time.Sleep (100 * time.Millisecond)}} () go func () {for {read ("k")}} () time.Sleep (10 * time.Second)}
Compared with read-write locks, there is less scramble for locks, but relatively, it brings some memory overhead, which is suitable for situations where there are more reads and less writes and variables do not take up a lot of memory. If you use memory to store a large amount of data, this is not suitable. Technically, it is mainly the common copy-on-write.
In addition, this storage is more suitable for program configuration, posting an official chestnut.
Var config Value / / holds current server configuration// Create initial config value and store into config.config.Store (loadConfig ()) go func () {/ / Reload config every 10 seconds / / and update config value with the new version. For {time.Sleep (10 * time.Second) config.Store (loadConfig ())} () / / Create worker goroutines that handle incoming requests// using the latest config value.for I: = 0; I < 10 Go func + {for r: = range requests () {c: = config.Load () / / Handle request r using config c. _, _ = r, c}} ()}
Yeah, it was fun.
The above is another way to play with golang value concurrency security. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
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
# = alarm mode 1 use Wechat official API = to be added # = alarm mode 2 =
© 2024 shulou.com SLNews company. All rights reserved.