In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to use Go RWMutex concurrency". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to use Go RWMutex concurrency.
RWMutex
RWMutex means read-write lock:
1. It allows arbitrary read operations to take place at the same time and is mainly used in scenarios where there are more reads and less writes. That is, when you modify the data, you need to be locked and read without locking.
two。 Multiple write operations under the control of read-write locks are mutually exclusive. When one goroutine acquires a write lock, other goroutine acquires read-write locks will wait.
3 when a goroutine acquires a read lock, other goroutine acquires a write lock, but when other goroutine acquires a read lock, it continues to acquire the lock.
To sum up: 1 only one goroutine can acquire write locks at the same time; 2 any number of goroutine can acquire read locks at the same time; 3 only write locks or read locks can exist at the same time (read and write mutually exclusive).
Locking and unlocking of write operations
/ / Lock sets rw to write-locked and forbids other routines from reading or writing. Func (rw * RWMutex) Lock () / / Unlock unlocks the write lock state of the rw, which will cause a panic if the rw is not written locked. Func (rw * RWMutex) Unlock ()
Locking and unlocking of read operations
/ / RLock sets rw to read-locked, which prevents other routines from writing, but can be read. Func (rw * RWMutex) RLock () / / Runlock unlocks the read state of the rw, which throws a panic if the rw is not read locked. Func (rw * RWMutex) RUnlock ()
Note:
Write unlock attempts to wake up all Goroutine blocked by read locking while it is in progress.
Read unlocking will only attempt to wake up a Goroutine that is blocked by write locking when there is no read lock in progress.
If you unlock a read-write lock that is not locked by a write, it will cause a panic at runtime.
However, this is not the case for reading and unlocking a read-write lock that is not read-locked.
In the scenario of more reading and less writing, the performance of read-write lock is higher than that of mutex lock.
Thank you for your reading, the above is the content of "how to use Go RWMutex concurrency". After the study of this article, I believe you have a deeper understanding of how to use Go RWMutex concurrency, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.