Data visibility on multi-core processor by single thread

China☆狼群 提交于 2019-12-06 05:20:28

Considering your first question, context switches also preserve the register contents. Therefore, the threads sees the latest value, even if moved to another core (or CPU).

However for a multi-threaded program, CPU registers are distinct for different threads (regardless on how many cores the threads are executed), and registers are not part of cache coherency.

Therefore, I think, a multi-threaded program does need to make sure the values in the registers are up-to-date with the values in the main memory. (Cache coherence only makes sure that the CPU cache is up-to-date with the memory). Therefore, I suppose, you need a barrier to synchronize the register with the memory.

You can understand it as this: the program essentially operates only on the main memory. However, compilers optimise access to main memory and use registers for intermediate operations. Thus, the program access only memory and registers. However, the CPU also introduces its own cache of the memory. Reads and writes from/to the memory are internally (by the CPU) optimised by the cache. Cache coherency only ensures within the CPU, that the cache is up-to-date (and therefore, a program accessing memory gets the correct value.)

To sum up:

  • Cache coherence ensures cache and memory are up-to-date, it is out of the control of the program, as it is internal to the CPU.
  • Context switches are handled by the operating system, which ensures correct values of registers when it moves threads to different cores.
  • Memory barriers ensure that the registers and memory are up-to-date, this is what the program has to ensure.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!