当前位置:网站首页>[volatile principle] volatile principle
[volatile principle] volatile principle
2022-07-27 02:10:00 【A duck that cannot enter the water】
volatile principle
One 、 What is? volatile
volatile yes Java A kind of Lightweight synchronization mechanism .Java Language contains two internal synchronization mechanisms : Synchronized block ( Or method ) and volatile Variable , comparison synchronized To solve the problem of shared memory ,volatile It's a lighter choice , He There is no additional overhead cost of context switching . Use volatile Declared variables , Yes When the hedge is updated, it is immediately visible to other threads , In a nutshell , It can lead to other CPU The corresponding cache value in is invalid .volatile Use memory barrier to ensure that instruction reordering does not occur , Solved the problem of memory visibility . however volatile Variable Poor synchronization ( Sometimes it's simpler and less expensive ), And its use is also more error prone .
Two 、volatile principle
Threads read shared variables from main memory to working memory for operation , Write the result to main memory after completion , But that brings visibility issues . for instance , Now let's say we're a two-level cache with dual cores CPU framework , contain L1、L2 Two level cache .
(1) Threads 1 First get the variable X Value , Because the first two levels of cache are empty , So read directly from main memory X, hypothesis X The initial value is 0, Threads 1 After reading, put X Values are changed to 1, Write back to main memory at the same time . At this time, the cache and main memory are shown in the following figure .
(2) Threads 2 Also read variables X Value , because L2 The cache already has a cache X=1, So directly from L2 Cache reads , After that thread 2 hold X It is amended as follows 2, At the same time, write back L2 And main memory . By this time X The value is shown in the figure below .
At this point, the thread 1 If you want to get a variable again X Value , because L1 The cache already has x=1 了 , So at this point Variable memory invisibility problem It came into being. , Threads 2 take X It is amended as follows 2 For threads 1 Come on No perception , The thread 1 I don't know at this time X The value of has become 2 了 .
however , If X Variable usage volatile Embellished words , When a thread 1 Read variables again X Words ,CPU The thread will be forced according to the cache consistency protocol 1 Reload the latest value from main memory to your working memory , Instead of just using the values in the cache .
stay JVM Bottom volatile Is to use Memory barrier To achieve . The memory barrier will provide 3 Features :
(1) It ensures that instructions are reordered without putting the instructions behind them in front of the memory barrier , It doesn't put the previous instructions behind the memory barrier ; That is, when the instruction of memory barrier is executed , All the operations before it have been completed ; In a nutshell , Execute to volatile variable , In front of the All statements have been executed , None of the following statements are executed . And the result of the previous statement is right volatile Variables and statements after them can be seen .
(2) It will Force changes to the cache to be written to main memory immediately ;
(3) If it's a write operation , it It can lead to other CPU The corresponding cache row in is invalid . Make sure When the value is updated, it is immediately visible to other threads .
This article is a study note , It is the understanding of others' knowledge , Together with some personal understandings of myself . If there is an infringement connection, delete . Writing is not easy to , I hope you guys can support me for three times . Be deeply grateful
notes : The source of the original text is https://mp.weixin.qq.com/s/-xFSHf7Gz3FUcafTJUIGWQ
边栏推荐
- MySQL master-slave replication and read-write separation
- Es specify user name and password when instantiating resthighlevelclient
- 为啥不建议使用Select *
- Introduction to network - Introduction to home networking & basic network knowledge
- 【mysql】mysql启动关闭命令以及一些报错解决问题
- R分数复现 R-precision评估指标定量 文本生成图像R分数定量实验全流程复现(R-precision)定量评价实验踩坑避坑流程
- ACM模式输入输出练习
- [reprint] 6. Tensorrt advanced usage
- 2022 open source work of the latest text generated image research (papers with code)
- Dynamic programming (knapsack problem)
猜你喜欢

MVCC及其原理详解

DF-GAN实验复现——复现DFGAN详细步骤 及使用MobaXtem实现远程端口到本机端口的转发查看Tensorboard

(atcoder contest 144) f - fork in the road (probability DP)

Difference between fat AP and thin AP & advantages and disadvantages of networking
![[paddleseg source code reading] paddleseg export static graph export Trace in py file](/img/63/ac9d1a569816e8addb23bf143364f5.png)
[paddleseg source code reading] paddleseg export static graph export Trace in py file

Text to image论文精读SSA-GAN:基于语义空间感知的文本图像生成 Text to Image Generation with Semantic-Spatial Aware GAN

(hdu1588) Gauss Fibonacci (sum of matrix fast power + bisection matrix proportional sequence)

Hash索引和B+树相关知识

Solution: various error reports and pit stepping and pit avoidance records encountered in the alchemist cultivation plan pytoch+deeplearning (II)

25pxe efficient batch network installation
随机推荐
FID index reproduction step on the pit to avoid the pit text generation image FID quantitative experiment whole process reproduction (FR é Chet inception distance) quantitative evaluation experiment s
【mysql】mysql启动关闭命令以及一些报错解决问题
dlib安装失败解决办法
[translation] reduced precision in tensorrt
Gan's training skills: alchemist cultivation plan - generative confrontation network training, participation and improvement
MySQL master-slave replication and read-write separation
三种能有效融合文本和图像信息的方法——特征拼接、跨模态注意、条件批量归一化
6.30联发科笔试
Timestamp conversion Greenwich mean time
TIM输出比较——PWM
Machine learning exercise 6 - Support Vector Machines
(codeforce 807div2)C. Mark and his unfinished essay (thinking)
mysql存储引擎及其区别
JS 99 multiplication table
left join 、inner join 、right join区别
6.28同花顺笔试
22FTP
ACM模式输入输出练习
Initial experience of cloud database management
Acwing 1057. stock trading IV