我的回答是告诉编译器不要对这个变量进行优化,但优化的英文当时实在是想不起来怎么说了,所以我说tell the compiler not to change it ,悲剧了 ------解决方案-------------------- tell the compiler not to change it 我感觉回答的也可以啊 ------解决方案-------------------- not optimize it since it's volatile ------解决方案-------------------- msdn says "A type qualifier that you can use to declare that an object can be modified in the program by the hardware" ------解决方案-------------------- 德国公司?? ------解决方案--------------------
the keyword volatile is used to ask hardware to get value of a viarant from memory, not from register.
这样回答可以不? ------解决方案-------------------- 如果是单线程环境,则这个volatile没有意义。
如果是多线程环境,则有volitate时,其他线程可以读出 i 的实时变化;无volatile时,其他线程会看到 i 值一直不变。
volatile迫使编译器每次都从内存里读取 i 的值,而不是尽可能从寄存器读取。
The volatile keyword does nothing if this programme is single-threaded, but in multi-threaded situation, volatile enables other threads to get the real-time value of 'i', without it, other threads may see the 'i' never changes.
Volatile makes the compiler to always fetch the value of a variable from memory instead of registers.