当前位置:网站首页>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 .
边栏推荐
- 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
- Traversal of binary tree -- restoring binary tree by two different Traversals
- QT Road (2) -- HelloWorld
- 6 questions to ask when selecting a digital asset custodian
- 【深入kotlin】 - Flow 进阶
- Concurrent search set
- MySQL nested sorting: first sort and filter the latest data, and then customize the sorting of this list
- Analyze while doing experiments -ndk article -jni uses registernatives for explicit method registration
- String sorting times --- bubble sorting deformation
- Wechat applet uploads the data obtained from database 1 to database 2
猜你喜欢

高斯白噪声(white Gaussian noise,WGN)

Paper reproduction: pare

Bert knowledge distillation

Deep learning distributed training

Preliminary understanding of multi task learning

Exploration of kangaroo cloud data stack on spark SQL optimization based on CBO

Zed2 running vins-mono preliminary test

es-ik 安装报错

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

22. Generate parentheses
随机推荐
高斯白噪声(white Gaussian noise,WGN)
微信小程序,购买商品属性自动换行,固定div个数,超出部分自动换行
Games101 job 7-path tracing implementation process & detailed interpretation of code
35.搜索插入位置
截取文件扩展名
Paper reproduction: pare
Share 𞓜 jointly pre training transformers on unpaired images and text
Combien de courant le câblage des PCB peut - il supporter?
How much current can PCB wiring carry
Retinanet+keras train their own data set to tread on the pit
Poverty has nothing to do with suffering
(十五)红外通信
Es IK installation error
6 questions to ask when selecting a digital asset custodian
White Gaussian noise (WGN)
Common methods of tool class objectutil
Combing route - Compaction Technology
[go deep into kotlin] - get to know flow for the first time
推荐一款免费的内网穿透开源软件,可以在测试本地开发微信公众号使用
自定义View之基础篇