当前位置:网站首页>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
}
边栏推荐
- 如何在自有APP内实现小程序实现连麦直播
- Case study on comprehensive competitiveness of principal components
- 《Go题库·16》读写锁底层是怎么实现的
- Leetcode-83 删除排序链表中重复的元素
- R语言ggplot2可视化:可视化折线图、使用labs函数为折线图添加自定义的Y轴标签信息(customize y axis label)
- 搭建一個通用監控告警平臺,架構上需要有哪些設計
- Unity learning fourth week
- 3、《创建您自己的NFT集合并发布一个Web3应用程序来展示它们》在本地铸造 NFT
- 力扣每日一题-第32天-1232. 缀点成线
- Qt中的QFile读写文件操作
猜你喜欢

Write an open source, convenient and fast database document query and generation tool with WPF

LiveData postValue会“丢”数据

11、用户、组和权限(1)

How to find the optimal learning rate

A database editing gadget that can edit SQLite database. SQLite database replaces fields. SQL replaces all values of a field in the database

What if the reliability coefficient is low? How to calculate the reliability coefficient?

OpenAI|视频预训练 (VPT):基于观看未标记的在线视频的行动学习

Relational database management system of easyclick

透过华为军团看科技之变(六):智慧公路

Must see, time series analysis
随机推荐
[image denoising] matlab code for removing salt and pepper noise based on fast and effective multistage selective convolution filter
每周推薦短視頻:警惕“現象”與“問題”相互混淆
bean的生命周期核心步骤总结
The R language uses the tablestack function of epidisplay package to make statistical summary tables (descriptive statistics based on the grouping of target variables, hypothesis testing, etc.). If th
Li Kou daily question - Day 32 -1232 Dotted line
Force buckle day33
搭建一个通用监控告警平台,架构上需要有哪些设计
数据仓库(四)之ETL开发
Altair HyperWorks 2022软件安装包和安装教程
Qt中的QFile读写文件操作
用GSConv+Slim Neck改进Yolov5,将性能提升到极致!
字节跳动数据平台技术揭秘:基于 ClickHouse 的复杂查询实现与优化
2、《创建您自己的NFT集合并发布一个Web3应用程序来展示它们》启动并运行您的本地环境
Memo - about C # generating barcode for goods
Salesmartly has some tricks for Facebook chat!
实现一个Prometheus exporter
R language uses the transmute function of dplyr package to calculate the moving window mean value of the specified data column in dataframe data, and uses ggplot2 package to visualize the line graph b
Popular science: what does it mean to enter the kernel state?
搭建一個通用監控告警平臺,架構上需要有哪些設計
540. Single element in ordered array / 1684 Count the number of consistent strings