当前位置:网站首页>Mutex and rwmutex in golang
Mutex and rwmutex in golang
2022-07-03 03:54:00 【sandyznb】
Mutex The mutex , The main means of access control for shared resources , It's easy to use , Just Lock and Unlock(), Simple use does not mean simple internal implementation , We can think deeply about if Mutex What happens when you unlock repeatedly ? Why does it show up ?
One :Mutex data structure
src\sync\mutex.go
// A Mutex must not be copied after first use.
type Mutex struct {
state int32
sema uint32
}
// A Locker represents an object that can be locked and unlocked.
type Locker interface {
Lock()
Unlock()
}state: Save the relevant data of the lock For example, the state of the lock 、 How many are waiting for this lock goroutine wait
sema: Semaphore , The coroutine block waits for the semaphore , The unlocked coroutine releases the semaphore to wake up the coroutine waiting for the semaphore .

locked: Indicates whether the current lock is locked ,0 It's not locked ,1 It's locked
woken: Is there a process that has been awakened ,0: There's no synergy 1: There is a process to wake up , In the process of locking
starving: Indicates whether the current lock is hungry ,0 No hunger ,1 Starvation It means that the blocking time of some processes has exceeded 1ms
waiter: The number of coprocesses blocking waiting locks , When unlocking the co process, judge whether to release the semaphore according to this value
Two : Add unlocking process
1 Simple locking : Suppose there is only one co process locking , No other co process interference ,locked From 0---->1, The locking process will judge locked Whether the flag bit is 0, If it is 0 Then put locked Set as 1, It represents the success of locking .
2 Locking is blocked : Suppose when locking , The lock has been occupied by other processes (locked It's already 1), here waiter The counter will be +1, The current process will be blocked , until locked become 0 Only then can we have a chance to be awakened
3 Simply unlock : Suppose there is no other co process blocking when unlocking ,locked from 1---->0, And there is no need to release semaphores
4 Unlock and wake up the process : Suppose when unlocking , Yes 1 One or more processes are blocked ,locked from 1--->0, here waiter>0, So release a semaphore , Wake up a blocked process , The awakened synergetic process Locked from 0--->1, The awakened process lives a lock
3、 ... and : The spin
When it's locked , If at present Locked It's already 1, It indicates that the lock has been occupied , The process of trying to lock does not immediately turn into blocking , It will continue to detect Locked Whether it becomes 0, This process is called spin process .
The spin time is very short , If you find that the lock has been released in the process of spinning , Then this process will immediately get all , At this time, even if other processes are awakened, the lock cannot be obtained , Can only block again .
The advantage of spin is that when locking fails, there is no need to immediately turn to blocking , There is a certain chance to get the lock , In this way, the switching of coordination process can be avoided .
The advantage of spin : Make better use of cpu, Try to avoid co process switching .
The problem with spin : If a lock is obtained during spin , Previously blocked processes will not get locks , If there are many locking processes and the lock is obtained by spinning every time , Then it is difficult for the blocked process to obtain the lock , Is in a state of hunger . This is the front starving, It doesn't spin in this state .
Four :Mutex Pattern
normal Patterns and starvation Pattern
The default state is normal, In this mode , If the process fails to lock, it will not immediately turn into the blocking queue , Instead, first judge whether the spin condition is satisfied , If satisfied, it will start the spin process and try to grab the lock .
边栏推荐
- Bisher - based on SSM pet adoption center
- [embedded module] OLED display module
- Recursion: depth first search
- Recursion: one dimensional linked lists and arrays
- 2.14 simulation summary
- Open Visual Studio 2010 hangs when opening a SQL file sql file
- [learning notes] seckill - seckill project - (11) project summary
- QQ小程序开发之 一些前期准备:预约开发账号、下载安装开发者工具、创建qq小程序
- 动态规划:最长公共子串和最长公共子序列
- 【刷题篇】接雨水(一维)
猜你喜欢

For instruction, uploading pictures and display effect optimization of simple wechat applet development
![Mongodb replication set [master-slave replication]](/img/2c/8030548455f45fa252062dd90e7b8b.png)
Mongodb replication set [master-slave replication]

leetcode:297. 二叉树的序列化与反序列化

在写web项目的时候,文件上传用到了smartupload,用了new string()进行转码,但是在数据库中,还是会出现类似扑克的乱码

Some preliminary preparations for QQ applet development: make an appointment for a development account, download and install developer tools, and create QQ applet

Simple wechat applet development page Jump, data binding, obtaining user information, obtaining user location information
![[Apple Photo Album push] IMessage group anchor local push](/img/a7/6a27d646ecba0d7c93f8dac38492a2.jpg)
[Apple Photo Album push] IMessage group anchor local push

Wechat applet + Alibaba IOT platform + Hezhou air724ug build a serverless IOT system (III) -- wechat applet is directly connected to Alibaba IOT platform aliiot

2022deepbrainchain biweekly report no. 104 (01.16-02.15)

【刷题篇】多数元素(超级水王问题)
随机推荐
编译文件时报错:错误: 编码GBK的不可映射字符
Read a paper_ ChineseBert
简易版 微信小程序开发之for指令、上传图片及展示效果优化
Arlo's thinking about himself
如何迈向IPv6之IPv6过渡技术-尚文网络奎哥
Half of 2022 is over, so we must hurry up
[mathematical logic] propositional logic (equivalent calculus | idempotent law | exchange law | combination law | distribution law | De Morgan law | absorption rate | zero law | identity | exclusion l
2022deepbrainchain biweekly report no. 104 (01.16-02.15)
[mathematical logic] propositions and connectives (propositions | propositional symbolization | truth connectives | no | conjunction | disjunction | non truth connectives | implication | equivalence)
redis在服务器linux下的启动的相关命令(安装和配置)
2.14 simulation summary
pytorch是什么?pytorch是一个软件吗?
Ffmpeg download and installation tutorial and introduction
[Blue Bridge Road -- bug free code] DS18B20 temperature reading code analysis
Recursive use and multi-dimensional array object to one-dimensional array object
【刷题篇】 找出第 K 小的数对距离
【刷题篇】接雨水(一维)
MySQL MAC download and installation tutorial
Applet get user avatar and nickname
[learning notes] seckill - seckill project - (11) project summary