当前位置:网站首页>《Go题库·16》读写锁底层是怎么实现的
《Go题库·16》读写锁底层是怎么实现的
2022-07-01 18:33:00 【GolangRoadmap】
*「题目解析」GOLANG ROADMAP社区
*
「答案」(自由)
读写锁的底层是基于互斥锁实现的。
为什么有读写锁,它解决了什么问题?(使用场景) 它的底层原理是什么?
在这里我会结合 Go 中的读写锁 RWMutex 进行介绍。
我们通过与 Mutex 对比得出答案。Mutex 是不区分 goroutine 对共享资源的操作行为的,在读操作、它会上锁,在写操作,它也会上锁,当一段时间内,读操作居多时,读操作在 Mutex 的保护下也不得不变为串行访问,对性能的影响也就比较大了。
RWMutex 读写锁的诞生为了区分读写操作,在进行读操作时,goroutine 就不必傻傻的等待了,而是可以并发地访问共享资源,将串行读变成了并行读,提高了读操作的性能。
读写锁针对解决一类问题:readers-writes ,同时有多个读或者多个写操作时,只要有一个线程在执行写操作,其他的线程都不能进行读操作。
读写锁其实有三种工作模型:
Read-perferring 优先读设计,可能会导致写饥饿 Write-prferring 优先写设计,避免写饥饿 不指定优先级 不区分优先级,解决饥饿问题
Go 中的读写锁,工作模型是 Write-prferring 方案。
「答案」(栾龙生)
读写锁解决问题
主要应用于写操作少,读操作多的场景。读写锁满足以下四条规则。
写锁需要阻塞写锁:一个协程拥有写锁时,其他协程写锁定需要阻塞; 写锁需要阻塞读锁:一个协程拥有写锁时,其他协程读锁定需要阻塞; 读锁需要阻塞写锁:一个协程拥有读锁时,其他协程写锁定需要阻塞; 读锁不能阻塞读锁:一个协程拥有读锁时,其他协程也可以拥有读锁。
读写锁底层实现
读写锁内部仍有一个互斥锁,用于将多个写操作隔离开来,其他几个都用于隔离读操作和写操作。
源码包
src/sync/rmmutex.go:RWMutex
中定义了读写锁的数据结构
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
}
边栏推荐
- Go Technology Daily (2022-02-14) - go language slice interview real questions 8 consecutive questions
- 证券开户安全么,有没有什么样的危险呢
- Bernoulli distribution (a discrete distribution)
- Navicat Premium 15 永久破解和2021版本最新IDEA破解(亲测有效)
- Search 2D matrix 2
- 如何运营好技术相关的自媒体?
- 540. Single element in ordered array / 1684 Count the number of consistent strings
- Is the fund of futures account safe? How to open an account?
- How to change guns for 2D characters
- Samba basic usage
猜你喜欢
The 13th simulation problem of the single chip microcomputer provincial competition of the Blue Bridge Cup
Highly reliable program storage and startup control system based on anti fuse FPGA and QSPI flash
创建您自己的NFT集合并发布一个Web3应用程序来展示它们(介绍)
t10_ Adapting to Market Participantsand Conditions
Android development interview was badly hit in 3 years, and now the recruitment technical requirements are so high?
About enterprise middle office planning and it architecture microservice transformation
NSI packaging script add file details
隐私沙盒终于要来了
Mujoco XML modeling
Search 2D matrix 2
随机推荐
Force buckle day33
Batch export all pictures in PPT in one second
解决方案:可以ping别人,但是别人不能ping我
Vidéos courtes recommandées chaque semaine: méfiez - vous de la confusion entre « phénomène » et « problème »
Image acquisition and playback of coaxpress high speed camera based on pxie interface
1、《创建您自己的NFT集合并发布一个Web3应用程序来展示它们》什么是NFT
[CF559E]Gerald and Path
Relational database management system of easyclick
LeetCode 148. Sort linked list
Write it down once Net travel management background CPU Explosion Analysis
Halcon图片标定,使得后续图片处理过后变成与模板图片一样
PTA year of birth
搭建一个通用监控告警平台,架构上需要有哪些设计
When the fixed frequency artifact falls in love with multithreading | ros2 fixed frequency topic release demo
Record 3 - the state machine realizes key control and measures the number of external pulses
Definition of rotation axis in mujoco
Navicat premium 15 permanent cracking and 2021 latest idea cracking (valid for personal testing)
Weekly recommended short videos: be alert to the confusion between "phenomena" and "problems"
Blue Bridge Cup real topic: the shortest circuit
LeetCode-21合并两个有序链表