当前位置:网站首页>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 .
边栏推荐
- 新功能|Mail GPU Counter模块新增GPU图元处理和GPU Shader Cycles
- Google Earth engine (GEE) -- batch download of DEM using MODIS leaf area index image mask
- What does the multi cloud management platform CMP mean? Who can explain it clearly
- 【云计算】多云管理平台和公有云两者之间是啥关系?
- 618. How to prepare for the great promotion
- [note] about the problem of insufficient compilation mapping memory in keil
- [Multisim Simulation] differential amplifier circuit 2
- markdown设置字体为红色
- What are the common automated test frameworks? Shanghai software testing company Amway
- C multithreading learning note 1
猜你喜欢

The relocation of Apple's production line shows that 5g industrial interconnection and intelligent manufacturing have limited help for manufacturing in China

2022大厂高频软件测试面试真题(附答案)

还在说大学排名是笑话?最新规定:世界top50大学可以直接落户上海!
![[vue/js] realize local caching of variables and objects through localstorage browser (text + full source code)](/img/4d/d6276955277942f96f11df3e4ecd4c.png)
[vue/js] realize local caching of variables and objects through localstorage browser (text + full source code)

【深度学习05】 交叉熵损失函数

40 necessary methodologies for large factories

多云管理平台cmp是什么意思?谁能清楚解释一下

What is the p value of a gene?

Google Earth Engine(GEE)——基于s2影像的实时全球10米土地利用/土地覆盖(LULC)数据集
![[C language] pointer function, function pointer and array function](/img/3c/e8816c4fe52c1757f4f9e17d19c285.png)
[C language] pointer function, function pointer and array function
随机推荐
About native SQL and database methods in PHP framework
Ten easy-to-use cross browser testing tools to share, good things worth collecting
Typescript introductory notes (personal)
Im instant messaging development: the underlying principle of process killed and app skills to deal with killed
22.6.7成功使用doc2vec模型生成嵌入向量
Smart campus security channel and video monitoring solution
技术分享| 快对讲,全球对讲
Flutter drawer学习总结6
格力手机叫板苹果手机?除了嘴硬之外,恐怕再无其他
【笔记】74HC573的一些记录
还在说大学排名是笑话?最新规定:世界top50大学可以直接落户上海!
618 大促来袭,浅谈如何做好大促备战
Flutter Wrap Button bottomNavigationBar学习总结4
[golang] when creating a structure with configuration parameters, how should the optional parameters be transferred?
【FAQ】运动健康服务REST API接口使用过程中常见问题和解决方法总结
「大模型」之所短,「知识图谱」之所长
The shortcomings of the "big model" and the strengths of the "knowledge map"
什么是CAS 以及 CAS 中的 ABA 问题
Tablayout usage details (modify text size, underline style, etc.)
C multithreading learning note 1