一句话解说内存屏障 Memory barrier
2016-06-21 18:30
399 查看
一句话解说内存屏障 Memory barrier 2011-03-17
16:39:02
分类: C/C++
内存屏障 Memory
barrier
By zieckey
All Right Reserved
内存屏障,可以保证在此之前的代码全部执行完才开始执行在此之后的代码
参考wikipedia的定义:
Memory barrier, also known as membar or memory
fence or fence instruction, is a type of barrier and
a class of instruction which
causes a central processing unit (CPU)
or compiler to enforce an ordering
constraint on memory operations
issued before and after the barrier instruction.
http://en.wikipedia.org/wiki/Memory_barrier
一个例子。
Processor #1:
x = f = 0
loop:
load the value in location f, if it is 0 goto loop
print the value in location x
Processor #2:
store the value 42 into location x
store the value 1 into location f
上面的例子中,变量 x f 初始化值都是0。我们期望输出“42”。但是结果并不总是这样。
如果 Processor #2 的执行顺序是乱序的,也就是说,对f赋值的语句先于对x赋值的语句,那么就有可能输出“0”
对于大多数的程序来说,这种特例情况是不能容忍的。
如果将内存屏障置于对f赋值的语句之前,那么就能保证 Processor #2 先对x赋值,然后才对f赋值。这样就能得到我们期望的结果,输出“42”
16:39:02
分类: C/C++
内存屏障 Memory
barrier
By zieckey
All Right Reserved
内存屏障,可以保证在此之前的代码全部执行完才开始执行在此之后的代码
参考wikipedia的定义:
Memory barrier, also known as membar or memory
fence or fence instruction, is a type of barrier and
a class of instruction which
causes a central processing unit (CPU)
or compiler to enforce an ordering
constraint on memory operations
issued before and after the barrier instruction.
http://en.wikipedia.org/wiki/Memory_barrier
一个例子。
Processor #1:
x = f = 0
loop:
load the value in location f, if it is 0 goto loop
print the value in location x
Processor #2:
store the value 42 into location x
store the value 1 into location f
上面的例子中,变量 x f 初始化值都是0。我们期望输出“42”。但是结果并不总是这样。
如果 Processor #2 的执行顺序是乱序的,也就是说,对f赋值的语句先于对x赋值的语句,那么就有可能输出“0”
对于大多数的程序来说,这种特例情况是不能容忍的。
如果将内存屏障置于对f赋值的语句之前,那么就能保证 Processor #2 先对x赋值,然后才对f赋值。这样就能得到我们期望的结果,输出“42”
相关文章推荐
- ubus数据结构和接口介绍
- Shell代码排序
- Java利用Sping框架编写RPC远程过程调用服务的教程
- 网页-资讯类图片异步加载
- QT Enum遍历
- Spring返回json乱码解决方法
- JSP中处理特殊字符
- OBJ模型文件的结构、导入与渲染Ⅰ
- 自定义抛出异常
- AWK的NR和FNR详解
- KahaDB简介
- 个人博客地址,http://devopslinux.com/
- PyQt5 第二篇 #应用程序图标
- ado显示,删除后刷新重新显示
- 9-9-B+树-查找-第9章-《数据结构》课本源码-严蔚敏吴伟民版
- 几个Java分隔后组装sql查询示例
- okhttp连接池复用机制
- 字符串拼接技术
- 编程之美2.10寻找数组中的最大值和最小值扩展问题Java版
- 认识nginx