当前位置:网站首页>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 .
边栏推荐
猜你喜欢

Paper reproduction: expressive body capture

Zed2 running vins-mono preliminary test

Combien de courant le câblage des PCB peut - il supporter?

1. use alicloud object OSS (basic)

初步了解多任务学习

PCB走線到底能承載多大電流

Sealem finance builds Web3 decentralized financial platform infrastructure

AAAI2022-ShiftVIT: When Shift Operation Meets Vision Transformer

Click the icon is not sensitive how to adjust?

自定义View基础之Layout
随机推荐
MySQL nested sorting: first sort and filter the latest data, and then customize the sorting of this list
MySQL circulates multiple values foreach, XML writing method
Bert knowledge distillation
[go deep into kotlin] - flow advanced
Games101 job 7-path tracing implementation process & detailed interpretation of code
Poverty has nothing to do with suffering
Share | defend against physically realizable image classification attacks
Linked list de duplication
Preliminary understanding of multi task learning
Click the icon is not sensitive how to adjust?
Section IV: composition and materials of asphalt mixture (1) -- structure composition and classification
22、生成括号
35.搜索插入位置
自定义View之基础篇
Sealem finance builds Web3 decentralized financial platform infrastructure
Wxparse parsing iframe playing video
27、移除元素
Target detection - personal understanding of RCNN series
Flask develops and implements the like comment module of the online question and answer system
The central rural work conference has released important signals. Ten ways for AI technology to help agriculture can be expected in the future