当前位置:网站首页>[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
边栏推荐
- GAN的训练技巧:炼丹师养成计划 ——生成式对抗网络训练、调参和改进
- MySQL master-slave replication and read-write separation
- mysql的安装
- [详解C语言]一文带你玩转循环结构(for_while_do-while)
- (atcoder contest 144) f - fork in the road (probability DP)
- Transport layer --------- TCP (II)
- [reprint] NVIDIA hardware and the accuracy mode supported by each hardware
- js求最大值?
- How can smart people leave without offending others?
- Solution: various error reporting and pit stepping and pit avoiding records encountered in the alchemist cultivation plan pytoch+deeplearning (III)
猜你喜欢

7.13 蔚来提前批笔试

7.16 多益网络笔试
![[reprint] GPU compute capability table](/img/7c/87be1131f52f21fe080c36f0834467.png)
[reprint] GPU compute capability table

MySQL master-slave replication and read-write separation

shell课程总结

Text to image intensive reading df-gan:a simple and effective baseline for text to image synthesis

MySQL single table query exercise

Codeforce problem 908 D. new year and arbitrary arrangement (probability DP)

MySQL多表查询

MySQL view
随机推荐
Share 29 chrome plug-ins, and there is always one for you
【mysql】mysql启动关闭命令以及一些报错解决问题
超出隐藏显示省略号
6.30联发科笔试
6.29 众安暑期测开实习一面
Hash索引和B+树相关知识
Introduction to network - Introduction to Enterprise Networking & basic knowledge of network
7.13 蔚来提前批笔试
[FPGA tutorial case 30] DDS direct digital frequency synthesizer based on FPGA -- frequency accuracy analysis with MATLAB
Freytek central computing platform 360 degree sensing system solves the challenges behind NOA mass production
Removal and addition of reference types in template and generic programming
[详解C语言]一文带你玩转函数
MySQL multi table query
Solution to high collapse
【volatile原理】volatile原理
Tinyint type map is received and returned as Boolean
Text to image论文精读RAT-GAN:文本到图像合成中的递归仿射变换 Recurrent Affine Transformation for Text-to-image Synthesis
Gan's training skills: alchemist cultivation plan - generative confrontation network training, participation and improvement
dlib安装失败解决办法
MVCC及其原理详解