当前位置:网站首页>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读与下面的普通写重排序。
边栏推荐
- 大小端模式
- ClickHouse: Setting up remote connections
- PMP WeChat group daily exercises
- Safety 20220722
- Can‘t load /home/Iot/.rnd into RNG
- Atomic operation CAS
- The application and practice of mid-to-platform brand advertising platform
- [Compilation principle] Design principle and implementation of recursive descent parsing
- $parent/$children 与 ref
- (八)Math 类、Arrays 类、System类、Biglnteger 和 BigDecimal 类、日期类
猜你喜欢
The application and practice of mid-to-platform brand advertising platform
一份高质量的测试用例如何养成?
浅识Flutter 基本组件之CheckBox组件
[Dynamic programming] Maximum sum of consecutive subarrays
LeetCode每日一练 —— OR36 链表的回文结构
【Exception】The field file exceeds its maximum permitted size of 1048576 bytes.
浅识Flutter 基本组件之showDatePicker方法
7年经验,功能测试工程师该如何一步步提升自己的能力呢?
Database implements distributed locks
type_traits元编程库学习
随机推荐
SIP协议标准和实现机制
(4) Recursion, variable parameters, access modifiers, understanding main method, code block
VS QT - ui does not display newly added members (controls) || code is silent
ClickHouse: Setting up remote connections
(Line segment tree) Summary of common problems of basic line segment tree
自己的一些思考
Safety 20220709
从滴滴罚款后数据治理思考
Redis uses sorted set to cache latest comments
[Swift]自定义点击APP图标弹出的快捷方式
$attrs/$listeners
The distance value between two arrays of LeetCode simple questions
Port inspection steps - 7680 port analysis - Dosvc service
Atomic operation CAS
[Compilation principle] Design principle and implementation of recursive descent parsing
Use of QML
interprocess communication
IDEA常用快捷键与插件
进程间通信
beforeDestroy与destroyed的使用