当前位置:网站首页>volatile内存语义以及实现 -volatile写和读对普通变量的影响
volatile内存语义以及实现 -volatile写和读对普通变量的影响
2022-07-31 03:38:00 【Jihu Tuo】
原文链接:https://blog.csdn.net/uhana/article/details/86592730
volatile内存语义以及实现
内存语义:
- 当写一个volatile变量时,JMM会把该线程对应的本地内存中的共享变量值立即刷新到主内存中(就是把L1,L2缓存刷新的L3上)。
- 当读一个volatile变量时,JMM会把该线程对应的本地内存设置为无效,直接从主内存中读取共享变量(直接从L3缓存读数据)。
实现:
volatile的底层实现是通过插入内存屏障,但是对于编译器来说,发现一个最优布置来最小化插入内存屏障的总数几乎是不可能的,所以,JMM采用了保守策略。如下:
- 在每一个volatile写操作前面插入一个StoreStore屏障
- 在每一个volatile写操作后面插入一个StoreLoad屏障
- 在每一个volatile读操作后面插入一个LoadLoad屏障
- 在每一个volatile读操作后面插入一个LoadStore屏障
StoreStore屏障可以保证在volatile写之前,其前面的所有普通写操作都已经刷新到主内存中。
StoreLoad屏障的作用是避免volatile写与后面可能有的volatile读/写操作重排序。
LoadLoad屏障用来禁止处理器把上面的volatile读与下面的普通读重排序。
LoadStore屏障用来禁止处理器把上面的volatile读与下面的普通写重排序。
边栏推荐
- The distance value between two arrays of LeetCode simple questions
- Mysql 45 study notes (twenty-five) MYSQL guarantees high availability
- [C language] General method for finding the sum of the greatest common factor and the least common multiple of two integers m and n, the classical solution
- SIP Protocol Standard and Implementation Mechanism
- 【C语言】预处理操作
- STM32 problem collection
- False positives and false negatives in testing are equally worthy of repeated corrections
- 安全20220718
- SocialFi 何以成就 Web3 去中心化社交未来
- LocalDate加减操作及比较大小
猜你喜欢
[Swift] Customize the shortcut that pops up by clicking the APP icon
SIP协议标准和实现机制
组件传值 provide/inject
The application and practice of mid-to-platform brand advertising platform
Detailed explanation of TCP (3)
web容器及IIS --- 中间件渗透方法1
Detailed explanation of TCP (2)
Daily practice of LeetCode - 138. Copy a linked list with random pointers
The distance value between two arrays of LeetCode simple questions
「 每日一练,快乐水题 」1331. 数组序号转换
随机推荐
(四)递归、可变参数、访问修饰符、理解 main 方法、代码块
Distributed locks and three implementation methods
What is a system?
7年经验,功能测试工程师该如何一步步提升自己的能力呢?
组件传值 provide/inject
安全20220709
Observer pattern
「 每日一练,快乐水题 」1331. 数组序号转换
3.5 】 【 Cocos Creator slow operating system to stop all animations
Unity2D 自定义Scriptable Tiles的理解与使用(四)——开始着手构建一个基于Tile类的自定义tile(下)
Atomic operation CAS
Several common errors when using MP
els 方块向右移
els 方块向右移动边界判断、向下加速
Day32 LeetCode
Redis实现分布式锁
els block to the right
Safety 20220712
Recursive query single table - single table tree structure - (self-use)
Mysql 45 study notes (twenty-four) MYSQL master-slave consistency