免费注册 查看新帖 |

Chinaunix

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

那为大侠给俺讲讲cache和volatie变量的关系 [复制链接]

论坛徽章:
0
1 [报告]
发表于 2008-03-04 21:54 |显示全部楼层

回复 #30 zx_wing 的帖子

cache一致不一定要写回内存,甚至不需要通过总线,比如双核系统共享写回式 L2 cache

论坛徽章:
0
2 [报告]
发表于 2008-03-07 10:19 |显示全部楼层

回复 #62 飞雪横天 的帖子

c++标准“貌似”没有线程的概念,volatile主要是对signal、setjmp/longjmp等

对与你这个全局变量我觉得应该使用明确的线程同步原语,用volatile可能不够

论坛徽章:
0
3 [报告]
发表于 2008-03-07 11:49 |显示全部楼层
这里有一个gcc-volatile的讨论http://gcc.gnu.org/ml/gcc/2007-10/msg00266.html


>   If you really want all externally-visible accesses to v to be made exactly
> as the code directs, rather than allowing gcc to optimise them in any way that
> (from the program's POV) it's just the same 'as-if' they had been done
> exactly, make v volatile.

That is not enough. Apart from the lack of ISO semantics for volatile,
typically a compiler will take volatile as a hint to not hold
values of the variable in a register.

On a multi-processor, this is not enough, because each CPU
may still hold modified values in separate caches.

Perhaps gcc actually puts a RW barrier to force
cache synchronisation on every volatile access..
this seems rather expensive and very hard to do since
it is very dependent on the actual box (not just the
processor). Some processor caches might require external
electrical signals to synchronise, for example. This is
quite possible if you have multiple CPU boards in a box.

But I don't actually know what gcc does, although I guess
it does nothing. The OS has to do the right thing here
when a mutex is locked etc, but the code for that is
probably in the kernel which is better able to manage
things like cache synchronisation than a compiler.

c/c++没有多线程的概念,没有一个明确的memory model,volatile不能保证共享变量在多线称间同步,
应该使用明确的lock。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP