免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12
最近访问板块 发新帖
楼主: zylthinking
打印 上一主题 下一主题

volatile 和 内存屏障 [复制链接]

论坛徽章:
0
11 [报告]
发表于 2011-11-02 00:28 |只看该作者
本帖最后由 sonicling 于 2011-11-02 00:45 编辑

volatile号称保证单CPU单个对象的访问顺序,但是Cache、乱序都有可能使它成为一句空话,结果volatile只剩下一个保证了,那就是保证读写不会被优化掉,这只是它的号称的一个附加效果。

内存屏障跟切换没关系,单x86下用不用内存屏障都没什么区别。内存屏障针对多CPU下,一个CPU等待另外一个CPU的结果。

论坛徽章:
6
金牛座
日期:2013-10-08 10:19:10技术图书徽章
日期:2013-10-14 16:24:09CU十二周年纪念徽章
日期:2013-10-24 15:41:34狮子座
日期:2013-11-24 19:26:19未羊
日期:2014-01-23 15:50:002015年亚洲杯之阿联酋
日期:2015-05-09 14:36:15
12 [报告]
发表于 2011-11-02 08:11 |只看该作者
回复 11# sonicling

单x86下用不用内存屏障都没什么区别。内存屏障针对多CPU下,一个CPU等待另外一个CPU的结果。

   
个人认为这句话应该是不正确的。在单CPU的情况下,也是需要内存屏障的。比如CPU和外设交互的情形。

论坛徽章:
2
CU十二周年纪念徽章
日期:2013-10-24 15:41:34处女座
日期:2013-12-27 22:22:41
13 [报告]
发表于 2011-11-02 08:48 |只看该作者
线程共用寄存器?
x86上cache需要软件担心一致性问题?
内存屏障?write buffer?

论坛徽章:
2
CU十二周年纪念徽章
日期:2013-10-24 15:41:34处女座
日期:2013-12-27 22:22:41
14 [报告]
发表于 2011-11-02 09:32 |只看该作者
回复 11# sonicling


   
Some devices present their control interfaces as collections of memory
locations, but the order in which the control registers are accessed is very
important.  For instance, imagine an ethernet card with a set of internal
registers that are accessed through an address port register (A) and a data
port register (D).  To read internal register 5, the following code might then
be used:

        *A = 5;
        x = *D;

but this might show up as either of the following two sequences:

        STORE *A = 5, x = LOAD *D
        x = LOAD *D, STORE *A = 5

the second of which will almost certainly result in a malfunction, since it set
the address _after_ attempting to read the register.

论坛徽章:
0
15 [报告]
发表于 2011-11-02 10:33 |只看该作者
回复 12# 瀚海书香
回复 14# tempname2


    的确。涉及到乱序,还是要靠内存屏障。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP