当前位置:网站首页>How to realize the bottom layer of read-write lock in go question bank 16
How to realize the bottom layer of read-write lock in go question bank 16
2022-07-01 18:51:00 【GolangRoadmap】
*「 title 」GOLANG ROADMAP Community
*
「 answer 」( free )
The bottom layer of read-write lock is based on mutual exclusion .
Why is there a read-write lock , What problem does it solve ?( Use scenarios ) What is its underlying principle ?
Here I will combine Go Read write lock in RWMutex To introduce .
We're going to talk to Mutex The answer comes from comparison .Mutex It's indistinguishable goroutine For the operation behavior of shared resources , Read operation 、 It will lock , Writing operations , It will also be locked , When for a while , Most of the reading operations , Read operation in Mutex Under the protection of, it has to become serial access , The impact on performance is relatively large .
RWMutex The birth of read-write lock is to distinguish read-write operations , When reading ,goroutine There is no need to wait foolishly , Instead, you can access shared resources concurrently , Turn serial reading into parallel reading , Improves the performance of read operations .
Read write locks are designed to solve a class of problems :readers-writes , When there are multiple read or write operations at the same time , As long as there is a thread performing write operations , Other threads cannot read .
There are actually three working models of read-write locks :
Read-perferring Read design first , May cause hunger Write-prferring Write design first , Avoid writing about hunger Don't assign priority Don't prioritize , Solve the problem of hunger
Go Read write lock in , The working model is Write-prferring programme .
「 answer 」( Luanlongsheng )
Read write lock solves the problem
Mainly used for less write operations , Scenarios with many read operations . Read / write locks meet the following four rules .
Write lock needs to block write lock : When a coroutine has a write lock , Other coroutine write locks need to be blocked ; Write lock needs to block read lock : When a coroutine has a write lock , Other coprocessor read locks need to be blocked ; Read lock needs to block write lock : When a coroutine has a read lock , Other coroutine write locks need to be blocked ; Read lock cannot block read lock : When a coroutine has a read lock , Other coroutines can also have read locks .
The bottom implementation of read-write lock
There is still a mutex inside the read-write lock , Used to isolate multiple writes , The others are used to isolate read and write operations .
Source package
src/sync/rmmutex.go:RWMutexThe data structure of read-write lock is defined in
type RWMutex struct {
w Mutex // held if there are pending writers
writerSem uint32 // semaphore for writers to wait for completing readers
readerSem uint32 // semaphore for readers to wait for completing writers
readerCount int32 // number of pending readers
readerWait int32 // number of departing readers
}
边栏推荐
- Lumiprobe 双功能交联剂丨Sulfo-Cyanine5 双-NHS 酯
- R language ggplot2 visualization: visualize the line graph and add customized Y-axis label information to the line graph using the labels function
- 3、《创建您自己的NFT集合并发布一个Web3应用程序来展示它们》在本地铸造 NFT
- Salesmartly has some tricks for Facebook chat!
- 斯坦福、Salesforce|MaskViT:蒙面视觉预训练用于视频预测
- 2. Create your own NFT collections and publish a Web3 application to show them start and run your local environment
- R language uses follow up of epidisplay package Plot function visualizes the longitudinal follow-up map of multiple ID (case) monitoring indicators, and uses n.of The lines parameter specifies the num
- Lumiprobe Lumizol RNA 提取试剂解决方案
- 如何运营好技术相关的自媒体?
- What if the reliability coefficient is low? How to calculate the reliability coefficient?
猜你喜欢

力扣每日一题-第32天-589.N×树的前序遍历

Create your own NFT collections and publish a Web3 application to show them (Introduction)

Force buckle day33

Unity learning fourth week

Leetcode-128 最长连续序列

Technology implementation and Architecture Practice
![[source code analysis] NVIDIA hugectr, GPU version parameter server - (1)](/img/92/f3a70e7086aeedf41eea3eef98b5aa.jpg)
[source code analysis] NVIDIA hugectr, GPU version parameter server - (1)

必看,时间序列分析

Leetcode-21 combines two ordered linked lists

Navicat Premium 15 永久破解和2021版本最新IDEA破解(亲测有效)
随机推荐
The R language cartools package divides the data, the scale function scales the data, the KNN function of the class package constructs the k-nearest neighbor classifier, and the table function calcula
Mise en place d'une plate - forme générale de surveillance et d'alarme, quelles sont les conceptions nécessaires dans l'architecture?
docker 部署mysql8.0
Go语言自学系列 | go语言数据类型
搭建一个通用监控告警平台,架构上需要有哪些设计
bean的生命周期核心步骤总结
Solution: you can ping others, but others can't ping me
Li Kou daily question - Day 32 -1232 Dotted line
12. Design of power divider for ads usage record
主成分计算权重
11、用户、组和权限(1)
Leetcode-83 delete duplicate elements in the sorting linked list
Five degrees easy chain enterprise app is newly upgraded
How to find the optimal learning rate
Mysql database of easyclick
Find all missing numbers in the array
R语言ggplot2可视化:可视化折线图、使用labs函数为折线图添加自定义的Y轴标签信息(customize y axis label)
Basic knowledge and commands of disk
R language uses the DOTPLOT function of epidisplay package to visualize the frequency of data points in different intervals in the form of point graph, and uses PCH parameters to customize the shape o
力扣每日一题-第32天-1232. 缀点成线