当前位置:网站首页>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
边栏推荐
- Concurrent search set
- IOU series (IOU, giou, Diou, CIO)
- 智能门锁为什么会这么火,米家和智汀的智能门锁怎么样呢?
- 深度学习分布式训练
- Stone game -- leetcode practice
- jvm调优六:GC日志分析和常量池详解
- Some details about memory
- Analyze while doing experiments -ndk article -jni uses registernatives for explicit method registration
- PCB走线到底能承载多大电流
- SwiftUI: Navigation all know
猜你喜欢

String sorting times --- bubble sorting deformation

Target detection - personal understanding of RCNN series

Paper recommendation: relicv2, can the new self supervised learning surpass supervised learning on RESNET?

Bert knowledge distillation

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

Click the icon is not sensitive how to adjust?

Share 𞓜 jointly pre training transformers on unpaired images and text

Deep extension technology: intelligent OCR recognition technology based on deep learning has great potential

NVIDIA SMI has failed because it could't communicate with the NVIDIA driver

Take stock of the AI black technologies in the Beijing Winter Olympic Games, and Shenzhen Yancheng Technology
随机推荐
Create cool collectionviewcell conversion animation
Conversion relationship between coordinate systems (ECEF, LLA, ENU)
Introduction to coordinate system in navigation system
深度学习分布式训练
Vins fusion GPS fusion part
QT Road (2) -- HelloWorld
Stone game -- leetcode practice
Oh my Zsh correct installation posture
wxParse解析iframe播放视频
MySQL nested sorting: first sort and filter the latest data, and then customize the sorting of this list
Carrier coordinate system inertial coordinate system world coordinate system
Flask develops and implements the like comment module of the online question and answer system
[go deep into kotlin] - flow advanced
Section II: structural composition characteristics of asphalt pavement (1) structural composition
Section IV: composition and materials of asphalt mixture (1) -- structure composition and classification
Feynman learning method
Project - Smart City
Wechat applet, automatic line feed for purchasing commodity attributes, fixed number of divs, automatic line feed for excess parts
推荐一款免费的内网穿透开源软件,可以在测试本地开发微信公众号使用
Number of atoms (easy to understand)