In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article Xiaobian for you to introduce in detail "golang concurrent security and lock how to achieve", the content is detailed, the steps are clear, the details are handled properly, I hope this article "golang concurrent security and lock how to achieve" article can help you solve your doubts, following the editor's ideas slowly in-depth, together to learn new knowledge.
Concurrency security and locking
Sometimes in Go code, there may be multiple goroutine operating on a resource (critical section) at the same time, which can lead to race problems (data race). Analogies to real-life examples include crossroads being competed by cars in all directions, and bathrooms on trains being competed by people in the carriages.
For example:
Var x int64var wg sync.WaitGroupfunc add () {for I: = 0; I < 5000; iTunes + {x = x + 1} wg.Done ()} func main () {wg.Add (2) go add () go add () wg.Wait () fmt.Println (x)}
In the above code, we turn on two goroutine to accumulate the value of the variable x. When the two goroutine access and modify the x variable, there will be data competition, resulting in the final result is not as expected.
Mutex import ("fmt"sync") var lock sync.Mutexlock.Lock () / lock lock.Unlock () / unlock
Mutex is a commonly used method to control access to shared resources, which ensures that only one goroutine can access shared resources at the same time. The Mutex type of sync package is used in the Go language to implement mutexes. Use mutexes to fix problems with the above code:
Var x int64var wg sync.WaitGroupvar lock sync.Mutexfunc add () {for I: = 0; I < 5000; iTunes + {lock.Lock () / lock x = x + 1 lock.Unlock () / / unlock} wg.Done ()} func main () {wg.Add (2) go add () go add () wg.Wait () fmt.Println (x)}
The use of mutex ensures that only one goroutine enters the critical section at the same time, and the other goroutine is waiting for the lock; when the mutex is released, the waiting goroutine can acquire the lock to enter the critical section, and when multiple goroutine wait for a lock at the same time, the wake-up strategy is random.
Read-write mutex import ("fmt"sync") var rwlock sync.RWMutexrwlock.Lock () / add-write lock rwlock.Unlock () / unwrite lock
Mutexes are completely mutually exclusive, but there are many practical scenarios where there are more reads and less writes. When we concurrently read a resource without resource modification, it is not necessary to add a lock. In this scenario, using read-write locks is a better choice. Read-write locks use the RWMutex type in the sync package in the go language.
There are two kinds of read-write locks: read locks and write locks. When a goroutine acquires a read lock, other goroutine will continue to acquire the lock if it acquires a read lock, and wait if it acquires a write lock; when a goroutine acquires a write lock, other goroutine will wait whether it acquires a read lock or a write lock.
Example of read-write lock:
Var (x int64 wg sync.WaitGroup lock sync.Mutex rwlock sync.RWMutex) func write () {/ / lock.Lock () / / add mutex rwlock.Lock () / / add write lock x = x + 1 time.Sleep (10 * time.Millisecond) / / assume that the read operation takes 10 milliseconds rwlock.Unlock () / / unlock / / lock.Unlock () / / Unmutex wg.Done ()} func read () {/ / lock.Lock () / / add mutex rwlock.RLock () / / add read lock time.Sleep (time.Millisecond) / / assume that the read operation takes 1 millisecond rwlock.RUnlock () / / unlock / / lock.Unlock () / / unlock mutex wg.Done ()} func main () {start: = time.Now () for I: = 0 I < 10; for + {wg.Add (1) go write ()} for I: = 0; I < 1000; I = wg.Add (1) go read ()} wg.Wait () end: = time.Now () fmt.Println (end.Sub (start))}
It should be noted that read-write locks are very suitable for scenarios with more read and write less. If there is little difference between read and write operations, the advantages of read-write locks can not be brought into full play.
After reading this, the article "how to implement golang concurrent Security and Lock" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, you are welcome to 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
© 2024 shulou.com SLNews company. All rights reserved.