C volatile variables and Cache Memory

前端 未结 7 1427
滥情空心
滥情空心 2020-11-29 20:17

Cache is controlled by cache hardware transparently to processor, so if we use volatile variables in C program, how is it guaranteed that my program reads data each time fro

相关标签:
7条回答
  • 2020-11-29 21:14

    From your question there is a misconception on your part.
    Volatile keyword is not related to the cache as you describe.

    When the keyword volatile is specified for a variable, it gives a hint to the compiler not to do certain optimizations as this variable can change from other parts of the program unexpectedly.

    What is meant here, is that the compiler should not reuse the value already loaded in a register, but access the memory again as the value in register is not guaranteed to be the same as the value stored in memory.

    The rest concerning the cache memory is not directly related to the programmer.

    I mean the synchronization of any cache memory of CPU with the RAM is an entirely different subject.

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