为什么会发生memory reorder
?一言以蔽之,因为性能。
在支持memory reorder
的系统上,有以下3
种order
需要考虑:
Program order: the order in which the memory operations are specified in the code running on a given CPU.
Execution order: the order in which the individual memory-reference instructions are executed on a given CPU. The execution order can differ from program order due to both compiler and CPU-implementation optimizations.
Perceived order: the order in which a given CPU perceives its and other CPUs’ memory operations. The perceived order can differ from the execution order due to caching, interconnect and memory-system optimizations. Different CPUs might well perceive the same memory operations as occurring in different orders.
Program order
是代码里访问内存的顺序。Execution order
是代码在CPU
上实际执行的顺序,由于编译器优化和CPU
的实现,实际指令执行的顺序有可能和代码顺序不一样。Perceived order
是CPU
用来“感知”自己或者其它CPU
对内存操作,由于caching
,interconnect
等原因,这个顺序有可能与代码实际的execution order
不同。
关于memory order
的总结:
A given CPU always perceives its own memory operations as occurring in program order. That is, memory-reordering issues arise only when a CPU is observing other CPUs’ memory operations.
An operation is reordered with a store only if the operation accesses a different location than does the store.
Aligned simple loads and stores are atomic.
Linux-kernel synchronization primitives contain any needed memory barriers, which is a good reason to use these primitives.
参考资料:
Memory Reordering Caught in the Act;
Memory Ordering in Modern Microprocessors, Part I。