Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use RWMutex in Go language

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Go language how to use RWMutex, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

1. Basic concept

Read-write lock: a synchronization mechanism for concurrency control of computer programs, also known as "shared-mutex lock" and multi-reader-single writer lock. Read operations can be reentered concurrently, while write operations are mutually exclusive.

The main applicable scenarios are: business scenarios that read more and write less. In this scenario, if mutexes are used for every read and write, the overall efficiency becomes very low. Because only reading does not need mutex to lock the data, only write operation needs mutex, and when reading and writing is combined, it also needs to be locked, otherwise the read data may not be expected.

The rules for RWMutex are as follows:

1. You can read multiple goroutine at the same time.

2. When writing, you can neither read nor write.

Mainly consists of the following four API, read lock RLock,RUnlock, write lock Lock,Unlock.

two。 Case analysis

For read-write locks, they can be split into the following four cases:

1) after the read event ends, the write event arrives. 2) during the read event, the write event arrives. 3) after the write event is completed, the read event arrives. 4) the read event arrives during the process of writing the event. 5) only the operation of reading. 6) only write operations.

For 1) and 3), read events and write events are executed sequentially without interacting with each other. Let's mainly look at the examples of 2), 4), 5), 6).

2.1 example 1, only read operations:

Result analysis: from the results of the run, we can see that multiple read operations do not need to wait until the end of the previous read event to execute the next read event, so once we modify the shared variable num between RLock and RUnlock, two cooperators 5 and 3 will read the same value 3.

2.2 example 2, only write operations:

Result analysis: only during the write operation, the result num of each execution is incremented, and there is no disorder, which shows that the locking and unlocking of the write lock are mutually exclusive, and the next execution must be completed after locking and unlocking.

2.3 example 3, during the read event, the write event arrives:

Result analysis: from the output above, we can see that the goroutinue of the write event starts at the end of the goroutinue of the second read data, because other read cooperators have been locked with read locks, so the write locks are not reexecuted until the read locks are released by these read protocols that have called the read locks.

2.4 example 4, during the writing process, the read event arrives:

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report