当前位置:网站首页>《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
}
边栏推荐
- Write an open source, convenient and fast database document query and generation tool with WPF
- Leetcode-128 最长连续序列
- R语言使用epiDisplay包的aggregate函数将数值变量基于因子变量拆分为不同的子集,计算每个子集的汇总统计信息
- Calculation of intersection of two line segments
- 540. Single element in ordered array / 1684 Count the number of consistent strings
- Technology implementation and Architecture Practice
- How to manage 1000 anchors by one person?
- PTA year of birth
- Find all missing numbers in the array
- Slider verification code identification gadget display
猜你喜欢

1380. Lucky number in matrix / 1672 Total assets of the richest customers

Leetcode203 移除链表元素
![[source code analysis] NVIDIA hugectr, GPU version parameter server - (1)](/img/92/f3a70e7086aeedf41eea3eef98b5aa.jpg)
[source code analysis] NVIDIA hugectr, GPU version parameter server - (1)

Implement a Prometheus exporter

因子分析怎么计算权重?

Definition of rotation axis in mujoco

GAMES202作业0-环境搭建过程&解决遇到的问题

Set the style of QT property sheet control

PCL learning materials

Calculation of intersection of two line segments
随机推荐
Using OpenSSL encryption to rebound shell traffic
About enterprise middle office planning and it architecture microservice transformation
Vue uses keep alive to cache page optimization projects
[source code analysis] model parallel distributed training Megatron (5) -- pipestream flush
[noip2015] jumping stone
How to change guns for 2D characters
每周推薦短視頻:警惕“現象”與“問題”相互混淆
Opencv map reading test -- error resolution
R语言使用epiDisplay包的followup.plot函数可视化多个ID(病例)监测指标的纵向随访图、使用n.of.lines参数指定显示的病例数
必看,时间序列分析
R语言使用epiDisplay包的aggregate函数将数值变量基于因子变量拆分为不同的子集,计算每个子集的汇总统计信息
MySQL connection tools
Bug of QQ browser article comment: the commentator is wrong
Financial judgment questions
R语言ggplot2可视化:gganimate创建动态柱状图动画(gif)、在动画中沿给定维度逐步显示柱状图、enter_grow函数和enter_fade函数控制运动内插退出(渐变tweening)
Happy new year | 202112 monthly summary
Three.js学习-相机Camera的基本操作(了解向)
A database editing gadget that can edit SQLite database. SQLite database replaces fields. SQL replaces all values of a field in the database
LiveData postValue会“丢”数据
力扣每日一题-第32天-1232. 缀点成线