当前位置:网站首页>Multi threading tutorial (XXIV) cas+volatile
Multi threading tutorial (XXIV) cas+volatile
2022-06-11 05:29:00 【Have you become a great God today】
Multithreading tutorial ( Twenty-four )CAS+volatile
When getting shared variables , To ensure the visibility of the variable , Need to use volatile modification .
It can be used to modify member variables and static member variables , It can avoid the thread looking up the value of the variable from its own working cache , You have to get its value in main memory , Thread operation volatile Variables operate directly on main memory . A thread pair volatile Modification of variables , Visible to another thread .
Be careful
volatile Only the visibility of shared variables is guaranteed , Let other threads see the latest value , But it can't solve the problem of instruction interleaving ( Atomicity is not guaranteed )
CAS Must use volatile To read the latest value of the shared variable 【 Compare and exchange 】 The effect of
As described in the previous section AtomticInteger Inside the method value the volatile modification . So there's no need for extra volatile modification .
Why is lockless efficient
Without a lock , Even if the retry fails , Threads are always running at high speed , There is no pause , and synchronized It will make the thread when it doesn't get the lock , A context switch has occurred , Get in the jam . Make a comparison
Threads are like racing cars on a high speed track , At high speed , Speed super fast , Once a context switch occurs , It's like a car has to slow down 、 Stalling , When you wake up, you have to start the fire again 、 start-up 、 Speed up … Return to high-speed operation , It costs a lot
But without lock , Because the thread needs to stay running , Additional requirements CPU Support for ,CPU It's like a high-speed track here , No extra runway , It's impossible for threads to run at high speed , Although it won't get into the jam , But because there is no time slice , It will still be operational , Or will it cause context switching .
CAS Characteristics
combination CAS and volatile Can achieve lock free concurrency , For less threads 、 Multicore CPU In the scene of .
CAS It's based on the idea of optimistic lock : The most optimistic estimate , Not afraid of other threads to modify shared variables , Even if it changes, it doesn't matter , I'll try again if I lose something .
synchronized It's based on the idea of pessimistic lock : The most pessimistic estimate , Prevent other threads from modifying shared variables , I'm locked. You don't want to change , I've changed it , You have a chance to .
CAS This is lock free concurrency 、 Non blocking concurrency , Please understand the meaning of these two sentences carefully
Because I didn't use synchronized, So threads don't get stuck , It's one of the factors in efficiency
But if the competition is fierce , It is conceivable that retrying must occur frequently , On the contrary, efficiency will be affected
边栏推荐
- 6 questions to ask when selecting a digital asset custodian
- SwiftUI: Navigation all know
- jvm调优六:GC日志分析和常量池详解
- Combination sum Ⅳ -- leetcode exercise
- Tightly coupled laser vision inertial navigation slam system: paper notes_ S2D. 66_ ICRA_ 2021_ LVI-SAM
- Combing route - Compaction Technology
- lower_ Personal understanding of bound function
- Take stock of the AI black technologies in the Beijing Winter Olympic Games, and Shenzhen Yancheng Technology
- JVM tuning 6: GC log analysis and constant pool explanation
- PCB走線到底能承載多大電流
猜你喜欢

1. use alicloud object OSS (basic)

White Gaussian noise (WGN)

WinForm (II) advanced WinForm and use of complex controls

自定义View基础之Layout

Thesis 𞓜 jointly pre training transformers on unpaired images and text

Technology | image motion drive interpretation of first order motion model

Stone game -- leetcode practice

es-ik 安装报错

Linked list de duplication

微信小程序text内置组件换行符不换行的原因-wxs处理换行符,正则加段首空格
随机推荐
Recursively process data accumulation
BERT知识蒸馏
BP neural network derivation + Example
Carrier coordinate system inertial coordinate system world coordinate system
Games101 job 7-path tracing implementation process & detailed interpretation of code
JVM tuning V: JVM tuning tools and tuning practice
Paper reproduction: expressive body capture
22. Generate parentheses
Redis setup (sentinel mode)
Maximum number of points on the line ----- hash table solution
Some details about memory
Recommend a free intranet penetration open source software that can be used in the local wechat official account under test
JS promise, async, await simple notes
Apply the intelligent OCR identification technology of Shenzhen Yanchang technology to break through the bottleneck of medical bill identification at one stroke. Efficient claim settlement is not a dr
PCB走線到底能承載多大電流
Paper reproduction: pare
Opencv learning path (2-3) -- Deep parsing imshow function
工具类ObjectUtil常用的方法
袋鼠云数栈基于CBO在Spark SQL优化上的探索
ROS compilation error: could not find a package configuration file provided by "XXX“