当前位置:网站首页>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读与下面的普通写重排序。
边栏推荐
- [Compilation principle] Lexical analysis program design principle and implementation
- 自己的一些思考
- Detailed explanation of TCP (3)
- Know the showTimePicker method of the basic components of Flutter
- VS QT - ui does not display newly added members (controls) || code is silent
- 进程间通信
- A brief introduction to the CheckboxListTile component of the basic components of Flutter
- Good place to download jar packages
- The BP neural network
- $attrs/$listeners
猜你喜欢
Understanding and Using Unity2D Custom Scriptable Tiles (4) - Start to build a custom tile based on the Tile class (below)
Daily practice of LeetCode - palindrome structure of OR36 linked list
A brief introduction to the CheckBox component of the basic components of Flutter
[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
【论文阅读】Mastering the game of Go with deep neural networks and tree search
$attrs/$listeners
「 每日一练,快乐水题 」1331. 数组序号转换
Daily practice of LeetCode - 138. Copy a linked list with random pointers
Know the showTimePicker method of the basic components of Flutter
$attrs/$listeners
随机推荐
errno错误码及含义(中文)
自己的一些思考
[Swift]自定义点击APP图标弹出的快捷方式
Postgresql 15 source code analysis (5) - pg_control
Redis counts new and retained users
Safety 20220712
Redis 统计用户新增和留存
WebSocket Session is null
No qualifying bean of type question
web容器及IIS --- 中间件渗透方法1
[C language] General method of expression evaluation
The application and practice of mid-to-platform brand advertising platform
C primer plus study notes - 8, structure
Zotero如何删除自动生成的标签
Redis uses LIST to cache the latest comments
Detailed explanation of TCP (2)
some of my own thoughts
LeetCode每日一练 —— 138. 复制带随机指针的链表
What is SQALE
IDEA common shortcut keys and plug-ins