C volatile variables and Cache Memory

前端 未结 7 1486
滥情空心
滥情空心 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条回答
  •  萌比男神i
    2020-11-29 21:12

    My suggestion is to mark the page as non-cached by the virtual memory manager.
    In Windows, this is done through setting PAGE_NOCACHE when calling VirtualProtect.

    For a somewhat different purpose, the SSE 2 instructions have the _mm_stream_xyz instructions to prevent cache pollution, although I don't think they apply to your case here.

    In either case, there is no portable way of doing what you want in C; you have to use OS functionality.

提交回复
热议问题