The Type Qualifier: Volatile

Declare global variables with volatile

 

Consider a handler and main routine that share a global variable g. The handler
updates g, and main periodically reads g. To an optimizing
compiler, it would appear that the value of g never changes in
main , and thus it would be safe to use a copy of g that is cached in
a register to satisfy every reference to g. In this case, the main
function would never see the updated values from the handler.
You can tell the compiler not to cache a variable by declaring it
with the volatile type qualifier. For example:
volatile int g;
The volatile qualifier forces the compiler to read the value of g
from memory each time it is referenced in the code. In general, as
with any shared data structure, each access to a global variable
should be protected by temporarily blocking signals.

 

Ref:

chapter 8.5.5, computer systems: a programmer's perspective, 3rd edition,

 

posted @ 2019-05-23 16:18  wordchao  阅读(270)  评论(0编辑  收藏  举报