当前位置:网站首页>What is CAS and ABA in CAS
What is CAS and ABA in CAS
2022-06-10 14:00:00 【Stars and dawn】
List of articles
In multithreading concurrency , To ensure the atomicity of threads .
The atomicity of atomic class is through volatile + CAS To achieve atomic operation .
Concept
CAS(Compare And Swap) To compare and exchange .CAS Algorithm CAS(V,E,N) contains 3 Parameters ,V Represents the variable to update ,E Indicates the expected value ,N Represents the new value .
In and only in V The value is equal to E When the value of , Will be V Value to N, If V Values and E Values are different , Indicates that other threads have updated , The current thread does nothing . Last ,CAS Returns the current V True value of .
This method is more efficient than locking , When V and E When the judgment is different , So the value is not updated , No blocking is sent , Continue to get CPU The enforcement of , Continue to judge and execute .

CAS Characteristics of : Optimism lock
CAS The idea of optimistic locking is adopted in the operation of , Always think that you can successfully complete the operation . When there are multiple threads that can be used at the same time CAS When you manipulate a variable , Only one will win and successfully update , The rest will fail .
The failed thread will not be suspended , Just being allowed to try again , Of course , It also allows the failed thread to abandon the operation . Based on this principle ,CAS Operation even without lock , You can also find the interference of other threads to the current thread , And deal with it properly .
CAS Spin waiting for
stay JDK The atomic package of java.util.concurrent.atomic A set of atomic classes is provided in , The basic feature of these atomic classes is in a multithreaded environment , When multiple threads execute the methods contained in the instances of these classes at the same time , There will be exclusivity . Its interior is based on CAS Algorithm to achieve , That is, when a thread enters the method and executes its instructions , Will not be interrupted by other thread locks ; And other threads are like spinlocks ,( Is to try again and again in the endless rotation ) Wait until the execution of the method is completed JVM Select a thread from the waiting queue .
be relative to synchronized Blocking algorithm , CAS Is a common implementation of non blocking algorithm . because CPU Context switching ratio CPU The operation of instruction set is more time-consuming , therefore CAS The performance of spin operation has been greatly improved .
CAS The shortcomings of
CAS Using a spin lock , Because the lock will be judged repeatedly , So there is no type synchronize Thread blocking causes thread switching .
But in the process of constant spin , It can lead to CPU Consumption of , Especially in the case of large concurrency, it is easy to cause CPU Run full .
In the use of CAS There is an important premise for the implementation of the algorithm : It is necessary to fetch the data in the memory at a certain time , Then the comparison was made immediately at one moment 、 Replace , Within this time difference, the data may have changed , It came into being. ABA problem .
ABA problem
ABA The problem is that the first thread from memory V Take out the position A , At this time, the second thread is also fetched from memory A And will V The data of the position is modified to B , And then V The data of the location is modified to A , This is the first time 1 Threads and then CAS In operation , What will happen in memory is still A , Then the first thread can operate successfully .
Although from the perspective of thread one ,CAS The operation of is successful , But in the process V The location data has changed , Just in the first thread , I didn't feel it , In some application scenarios, process data inconsistency may occur .
So to solve this ABA The problem of , In some optimistic locks , Yes by version number (version) To solve ABA The problem of .
Specific operation : The optimistic lock will carry a version number every time it performs data modification , When the expected version number is consistent with the data version number, you can perform the modification operation , And add... To the version number 1 The operation of , Otherwise, the execution fails . Therefore, the version number will increase with each operation , So there won't be ABA problem . Because the version number will only increase or not decrease .
Code demonstration :
Here in thread 2 , It should not be changed , This is where the ABA The problem of , So here we are going to solve this ABA The problem of .
边栏推荐
- 大厂面试上午10:00面试,10:09就出来了 ,问的实在是太...
- 【C语言】指针函数与函数指针、数组函数
- 【解决】每次加载已经训练好的模型,生成的向量会有不同
- buuctf [Discuz]wooyun-2010-080723
- Flutter drawer学习总结6
- net core天马行空系列-可用于依赖注入的,数据库表和c#实体类互相转换的接口实现
- Notes - simple but adequate series_ The Yapi return parameter data should be an object type problem solving record
- Mmdetection adds precision to the evaluation index
- 解决跨海高并发崩溃难题?so easy
- Pycharm安装详细教程
猜你喜欢

传奇登录器提示连接服务器失败是怎么回事?怎么解决?
![[FAQ] résumé des problèmes courants et des solutions lors de l'utilisation de l'interface API rest du Service de santé sportive](/img/ff/96a0a77795b271bef3a8ade2d646c3.png)
[FAQ] résumé des problèmes courants et des solutions lors de l'utilisation de l'interface API rest du Service de santé sportive

《软件体系结构原理、方法与实践》第二版期末考试复习总结

Flutter Icon Stack LIsttitle...学习总结3
![[note] about the problem of insufficient compilation mapping memory in keil](/img/0d/789c7629823600dbc77e62744ef5a1.png)
[note] about the problem of insufficient compilation mapping memory in keil

【FAQ】運動健康服務REST API接口使用過程中常見問題和解决方法總結

The interview at the big factory was held at 10:00 a.m. and came out at 10:09 a.m. the question was really too

What can the graph of training and verification indicators tell us in machine learning?

大厂面试上午10:00面试,10:09就出来了 ,问的实在是太...
![[deep learning 05] cross entropy loss function](/img/00/7bf13cb86324f1e9942ce45d5cc13c.png)
[deep learning 05] cross entropy loss function
随机推荐
解决VMWareStation安装 tools 时 D:\setup.exe 找不到的问题
How to solve the problem that vmware tools are grayed out when VMware Workstation is installed
What does the multi cloud management platform CMP mean? Who can explain it clearly
What needs to be done for mobile app performance testing? How much is the performance test report charged?
NC|王军/宋默识结合三代测序解析肠道菌群结构变异和功能
【技术分析】探讨大世界游戏的制作流程及技术——前期流程篇
Gorm设置外键
5.8G微波雷达模块使用,5.8G微波雷达模块工作原理和介绍
【FAQ】運動健康服務REST API接口使用過程中常見問題和解决方法總結
五角大楼首次承认资助46个乌生物设施 俄方曾曝只有3个安全
About native SQL and database methods in PHP framework
为doc2vec生成训练向量的数据集
多云管理平台cmp是什么意思?谁能清楚解释一下
typescript入门笔记(个人用)
格力手机叫板苹果手机?除了嘴硬之外,恐怕再无其他
SnackBar usage details
Typescript introductory notes (personal)
Notes - simple but adequate series_ The Yapi return parameter data should be an object type problem solving record
Pychart installation tutorial
Smart campus security channel and video monitoring solution