首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

widnows上的volatile是否修饰结构体、类,不管用

2012-11-09 
widnows下的volatile是否修饰结构体、类,不管用啊一个线程读取一个结构体一个线程修改一个结构体写线程修改

widnows下的volatile是否修饰结构体、类,不管用啊
一个线程读取一个结构体

一个线程修改一个结构体


写线程修改后, 但是读线程可能读取到的数据是寄存器的吧(这种可能性为0, 结构体一般都大于4字节的)

,寄存器无法保存整个结构体。所以优化不了 它。



对于32味或者小于32位的共享变量,用 volatile,才管用吧?





[解决办法]
给个实例:

C/C++ code
struct Stru{    double d1;    int i;    double d2;};int main(){    const Stru x={3.7,4,4.8};    cout <<x.i <<endl;            return 0;}
[解决办法]
探讨
你理解了?

侯捷的书 不会无辜讲解volatile吧?



引用:

明显lz没理解volatile的真正用法,跟锁不搭噶!

[解决办法]
volatile
volatile declarator

The volatile keyword is a type qualifier used to declare that an object can be modified in the program by something other than statements, such as the operating system, the hardware, or a concurrently executing thread.

The following example declares a volatile integer nVint whose value can be modified by external processes:

int volatile nVint;
Objects declared as volatile are not used in optimizations because their value can change at any time. The system always reads the current value of a volatile object at the point it is requested, even if the previous instruction asked for a value from the same object. Also, the value of the object is written immediately on assignment. 

One use of the volatile qualifier is to provide access to memory locations used by asynchronous processes such as interrupt handlers.

For related information, see const.

热点排行