当前位置:网站首页>《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
}
边栏推荐
- [Chongqing Guangdong education] basic psychology reference materials of Tianjin Normal University
- 必看,时间序列分析
- Highly reliable program storage and startup control system based on anti fuse FPGA and QSPI flash
- Halcon image calibration enables subsequent image processing to become the same as the template image
- Introduction to easyclick database
- Terms related to K line
- Leetcode-83 删除排序链表中重复的元素
- Technology implementation and Architecture Practice
- Convert the robot's URDF file to mujoco model
- What if the reliability coefficient is low? How to calculate the reliability coefficient?
猜你喜欢

Leetcode problem solving series -- continuous positive sequence with sum as s (sliding window)

隐私沙盒终于要来了

Leetcode-83 删除排序链表中重复的元素

Domestic spot silver should be understood

Lumiprobe 双功能交联剂丨Sulfo-Cyanine5 双-NHS 酯

Flex layout

3、《创建您自己的NFT集合并发布一个Web3应用程序来展示它们》在本地铸造 NFT

How to find customers for investment attraction in industrial parks

Mise en place d'une plate - forme générale de surveillance et d'alarme, quelles sont les conceptions nécessaires dans l'architecture?

Implement a Prometheus exporter
随机推荐
Memo - about C # generating barcode
Mujoco XML modeling
Growing up in the competition -- (Guangyou's most handsome cub) Pikachu walking
Samba basic usage
[acnoi2022] color ball
Sum of three numbers
Depth first search - DFS (burst search)
How to find the optimal learning rate
Blue Bridge Cup real question: score statistics
[noip2015] jumping stone
Computer network interview assault
Sanfeng cloud 0215 I often use
Flex layout
Find all missing numbers in the array
Leetcode203 移除链表元素
Operation of cmake under win
Mysql database of easyclick
Vidéos courtes recommandées chaque semaine: méfiez - vous de la confusion entre « phénomène » et « problème »
Happy new year | 202112 monthly summary
Mujoco's biped robot Darwin model