当前位置:网站首页>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:RWMutex
The 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
}
边栏推荐
- ACM MM 2022视频理解挑战赛视频分类赛道冠军AutoX团队技术分享
- 宏观视角看抖音全生态
- Leetcode-21 combines two ordered linked lists
- Lumiprobe bifunctional crosslinker sulfo cyanine 5 bis NHS ester
- 斯坦福、Salesforce|MaskViT:蒙面视觉预训练用于视频预测
- GAMES202作业0-环境搭建过程&解决遇到的问题
- ES6数组方法find()、findIndex()的总结「建议收藏」
- Leetcode-160 intersecting linked list
- 必看,时间序列分析
- 记一次 .NET 差旅管理后台 CPU 爆高分析
猜你喜欢
Go Technology Daily (2022-02-14) - go language slice interview real questions 8 consecutive questions
Sum of three numbers
How does factor analysis calculate weights?
毕业季 | 华为专家亲授面试秘诀:如何拿到大厂高薪offer?
主成分计算权重
Leetcode-128 longest continuous sequence
After studying 11 kinds of real-time chat software, I found that they all have these functions
因子分析怎么计算权重?
Why do independent website sellers start to do social media marketing? The original customer conversion rate can be improved so much!
[image denoising] matlab code for removing salt and pepper noise based on fast and effective multistage selective convolution filter
随机推荐
1. "Create your own NFT collections and publish a Web3 application to show them." what is NFT
数据库基础:select基本查询语句
《Go题库·16》读写锁底层是怎么实现的
Image acquisition and playback of coaxpress high speed camera based on pxie interface
Halcon图片标定,使得后续图片处理过后变成与模板图片一样
R语言使用epiDisplay包的aggregate函数将数值变量基于因子变量拆分为不同的子集,计算每个子集的汇总统计信息
Go Technology Daily (2022-02-14) - go language slice interview real questions 8 consecutive questions
R language ggplot2 visualization: gganimate package transition_ Time function to create dynamic scatter animation (GIF), shadow_ The wake function configures the gradient falloff tailing effect of the
Five degrees easy chain enterprise app is newly upgraded
R语言ggplot2可视化:gganimate创建动态柱状图动画(gif)、在动画中沿给定维度逐步显示柱状图、enter_grow函数和enter_fade函数控制运动内插退出(渐变tweening)
关于企业中台规划和 IT 架构微服务转型
12 data dimensioning processing methods
Popular science: what does it mean to enter the kernel state?
Using OpenSSL encryption to rebound shell traffic
GameFramework食用指南
How to change guns for 2D characters
Salesmartly has some tricks for Facebook chat!
Facebook聊单,SaleSmartly有妙招!
Database foundation: select basic query statement
Create your own NFT collections and publish a Web3 application to show them (Introduction)