Does volatile qualifier cancel caching for this memory?

后端 未结 2 1093
梦毁少年i
梦毁少年i 2021-02-06 02:18

In this article: http://www.drdobbs.com/parallel/volatile-vs-volatile/212701484?pgno=2 says, that we can\'t do any optimization for volatile, even such as (where: <

相关标签:
2条回答
  • 2021-02-06 02:45

    volatile ensures that the variable won't be "cached" in CPU register. CPU cache is transparent to the programmer and if another CPU writes to the memory mapped by another CPU's cache, the second CPU's cache gets invalidated, therefore it will reload the value from the memory again during the next access.

    Something about Cache coherence

    As for the external memory writes (via DMA or another CPU-independent channel), you might need to flush the cache manually (see this SO question)


    C Standard §6.7.3 7:

    What constitutes an access to an object that has volatile-qualified type is implementation-defined.

    0 讨论(0)
  • 2021-02-06 02:50

    The semantics of volatile are implementation-defined. If a compiler knew that interrupts would be disabled while certain piece of code was executed, and knew that on the target platform there would be no means other than interrupt handlers via which operations on certain storage would be observable, it could register-cache volatile-qualified variables within such storage just the same as it could cache ordinary variables, provided it documented such behavior.

    Note that what aspects of behavior are counted as "observable" may be defined in some measure by the implementation. If an implementation documents that it is not intended for use on hardware which uses main RAM accesses to trigger required externally-visible actions, then accesses to main RAM would not be "observable" on that implementation. The implementation would be compatible with hardware which was capable of physically observing such accesses, if nothing cared whether any such accesses were actually seen. If such accesses were required, however, as they would be if the accesses were regarded as "observable", however, the compiler would not be claiming compatibility and would thus make no promise about anything.

    0 讨论(0)
提交回复
热议问题