免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 63537 | 回复: 6
打印 上一主题 下一主题

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

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-02-29 13:44 |显示全部楼层 |倒序浏览
上次面试被考官问到volatie变量,他说这个要结合cache来讲才能说的清,那位大侠给讲一下。

论坛徽章:
0
2 [报告]
发表于 2008-02-29 15:24 |显示全部楼层
多谢zx_wing 和tinywind两位大侠给的解释,本来一头雾水,现在终于有点了解了,但是有些不明白的就是zx_wing说到了两个CPU,这个我不是很理解了,

论坛徽章:
0
3 [报告]
发表于 2008-02-29 16:13 |显示全部楼层
还是CU上面高手多啊,在其他几个坛子提了半天,都没有几个让我明白的答案,以后就觉得泡CU了

论坛徽章:
0
4 [报告]
发表于 2008-02-29 16:15 |显示全部楼层
还是CU上面高手多啊,在几个坛子里面发了都没有几个让我明白的回答,以后决定就泡CU了

论坛徽章:
0
5 [报告]
发表于 2008-02-29 16:21 |显示全部楼层
也就是说cache的内容同步有硬件机制完成,而volatile则是编译器优化的时候保证其变量的同步?

我这样理解可以不?

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

回复 #39 sohu2000000 的帖子

OK,谢谢咯

论坛徽章:
0
7 [报告]
发表于 2008-03-04 16:29 |显示全部楼层
刚才在sun网站上找到一个关于volatile变量的文章,感觉说的挺好的了。


Declaring a Variable Volatile


volatile is a keyword that must be applied when declaring any variable that will reference a device register. Without the use of volatile, the compile-time optimizer can inadvertently delete important accesses. Neglecting to use volatile might result in bugs that are difficult to track down.

The correct use of volatile is necessary to prevent elusive bugs. The volatile keyword instructs the compiler to use exact semantics for the declared objects, in particular, not to remove or reorder accesses to the object. Two instances where device drivers must use the volatile qualifier are:

When data refers to an external hardware device register, that is, memory that has side effects other than just storage. Note, however, that if the DDI data access functions are used to access device registers, you do not have to use volatile.

When data refers to global memory that is accessible by more than one thread, that is not protected by locks, and that relies on the sequencing of memory accesses. Using volatileconsumes fewer resources than using lock.

The following example uses volatile. A busy flag is used to prevent a thread from continuing while the device is busy and the flag is not protected by a lock:

    while (busy) {
      /* do something else */
      }The testing thread will continue when another thread turns off the busy flag:

    busy = 0;Because busy is accessed frequently in the testing thread, the compiler can potentially optimize the test by placing the value of busy in a register and test the contents of the register without reading the value of busy in memory before every test. The testing thread would never see busy change and the other thread would only change the value of busy in memory, resulting in deadlock. Declaring the busy flag as volatile forces its value to be read before each test.


--------------------------------------------------------------------------------
Note –
An alternative to the busy flag is to use a condition variable. See Condition Variables in Thread Synchronization.


--------------------------------------------------------------------------------

When using the volatile qualifier, avoid the risk of accidental omission. For example, the following code

    struct device_reg {
     volatile uint8_t csr;
     volatile uint8_t data;
     };
     struct device_reg *regp;is preferable to the next example:

    struct device_reg {
     uint8_t csr;
     uint8_t data;
     };
     volatile struct device_reg *regp;Although the two examples are functionally equivalent, the second one requires the writer to ensure that volatile is used in every declaration of type struct device_reg. The first example results in the data being treated as volatile in all declarations and is therefore preferred. As mentioned above, using the DDI data access functions to access device registers makes qualifying variables as volatile unnecessary.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP