zoukankan      html  css  js  c++  java
  • 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,

  • 相关阅读:
    第二周总结
    币值转换
    抓老鼠
    秋季学习总结
    第七周作业
    第六周作业
    2019年春季第五周
    2019年春季学期第四周作业
    2019年春季学期第三周作业
    2019年春季学期第二周作业
  • 原文地址:https://www.cnblogs.com/wordchao/p/10912588.html
Copyright © 2011-2022 走看看