C volatile variables and Cache Memory

前端 未结 7 1499
滥情空心
滥情空心 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:11

    volatile makes sure that data is read everytime it is needed without bothering with any cache between CPU and memory. But if you need to read actual data from memory and not cached data, you have two options:

    • Make a board where said data is not cached. This may already be the case if you address some I/O device,
    • Use specific CPU instructions that bypass the cache. This is used when you need to scrub memory for activating possible SEU errors.

    The details of second option depend on OS and/or CPU.

提交回复
热议问题