当前位置:网站首页>Mutex lock, read / write lock, spin lock, pessimistic lock, and optimistic lock
Mutex lock, read / write lock, spin lock, pessimistic lock, and optimistic lock
2022-06-30 14:09:00 【Zhen Jie.】
The mutex
When a thread wants to access a shared resource ( Critical resources ) Before , The code that will be accessed by the thread ( A critical region ) To lock . If other threads want to access critical resources after locking and before releasing the lock , These threads will be blocked , Until it's unlocked , If one or more threads block when unlocking , Then the threads on the lock become ready , Then the first thread that becomes ready gets the right to use the resource , And lock it again , Other threads continue to block and wait .
characteristic :
Only one thread can have a mutex at a time , Other threads have to wait
Application scenarios :
Mutexes serialize operations , It can ensure complete and orderly operation , It is suitable for the situation that resources can only be operated by one co process , Weak concurrency ;
Read-write lock
Also called shared mutex , Read mode sharing , Write patterns are mutually exclusive . It's a bit like the read-write separation mode of database load balancing . It has three modes : Read lock status , Write locked state and unlocked state . In short, only one thread can hold the read-write lock in write mode , But there can be multiple threads occupying the read-write lock in read mode .
When writing in lock mode , Any thread that locks it will be blocked , Until it's unlocked .
When in read lock mode , Any thread can read and lock it , But all threads that try to write lock are blocked . Until all read threads are unlocked . But when there are too many read threads , It's obviously not right that the write thread has been blocked all the time , So when a thread wants to write and lock it , Will block read and lock , Let the write lock thread lock first
characteristic :
1) Multiple readers can read at the same time
2) Writers must be mutually exclusive ( Only one writer is allowed to write , It can't be done by readers and writers at the same time )
3) The writer takes precedence over the reader ( Once there are writers , Then subsequent readers must wait , When waking up, priority should be given to the writer )
Application scenarios :
The read-write lock is suitable for reading more and writing less , Concurrency can be strong
spinlocks
Spinlocks and mutexes are very similar , The only difference is when the spinlock accesses the locked resource , Will always cycle to see if the lock is released . This is much more efficient than mutex , But it still needs to be occupied CPU. So spin locking can be applied to multinuclear CPU. But there is also a problem that when a spin lock is called recursively, it will cause deadlock . So use the spin lock carefully .
characteristic :
Only one process can enter the critical zone at a time , Read / write lock is a special case of spin lock .
Optimism lock
This is actually an idea , When the thread goes to get the data , Think other threads won't modify data , It's not locked , However, when updating the data, the version will be used to determine whether other threads have modified the data ., If the data is modified, the update is rejected , It's called optimistic lock because there's no lock .
Pessimistic locking
When the thread goes to get the data , It is always assumed that other threads will modify data , So it locks every time it takes data , Other threads will block when they go to get data .
These two types of locks are generally used in databases , When the number of read operations of a database is much larger than the number of write operations , Using optimistic locks increases database throughput .
The mutex 、 spinlocks 、 Read-write lock , All belong to Pessimistic locking
边栏推荐
猜你喜欢

Google Earth Engine(GEE)——将字符串的转化为数字并且应用于时间搜索( ee.Date.fromYMD)

This article explains the concepts of typed array, arraybuffer, typedarray, DataView, etc

可觀測,才可靠:雲上自動化運維CloudOps系列沙龍 第一彈

Shell programming overview

智慧运维:基于 BIM 技术的可视化管理系统

Intelligent operation and maintenance: visual management system based on BIM Technology

Pytoch viewing model parameter quantity and calculation quantity

Observable, seulement fiable: première bombe de salon de la série cloudops d'exploitation et d'entretien automatisés dans le nuage

visualstudio 和sql

QQ 居然被盗了?原因在这......
随机推荐
【刷题篇】爱吃香蕉的珂珂
想请教一下,我在佛山,到哪里开户比较好?手机开户是安全么?
Solve the error in my QT_ thread_ global_ End(): 3 threads didn't exit
想請教一下,我在佛山,到哪裏開戶比較好?手機開戶是安全麼?
Basic syntax of unity script (1) - common operations of game objects
Defi "where does the money come from"? A problem that most people don't understand
Tencent two sides: @bean and @component are used on the same class. What happens?
DNS 解析之家庭网络接入 Public DNS 实战
提权扫描工具
单元测试效率优化:为什么要对程序进行测试?测试有什么好处?
I'd like to ask you, where can I open an account in Foshan? Is it safe to open a mobile account?
服务线上治理
[observation] as the intelligent industry accelerates, why should AI computing power take the lead?
Unity animator parameter
[Title brushing] avoid flooding
Yousi College: Six Sigma is not just statistics!
exlipse同时操作多行。比如同时在多行同列输入相同的文字
Problems in QT creator (additional unknown and error lines are listed in the debug output window)
Introduction to the renewal of substrate source code: the pallet alliance is incorporated into the main line,
知识传播不能取代专业学习!