This article has multiple issues. Please help improve it or discuss these issues on the talk page. (Learn how and when to remove these messages)
|
Memory ordering is the order of accesses to computer memory by a CPU. Memory ordering depends on both the order of the instructions generated by the compiler at compile time and the execution order of the CPU at runtime.[1][2] However, memory order is of little concern outside of multithreading and memory-mapped I/O, because if the compiler or CPU changes the order of any operations, it must necessarily ensure that the reordering does not change the output of ordinary single-threaded code.[1][2][3]
The memory order is said to be strong or sequentially consistent when either the order of operations cannot change or when such changes have no visible effect on any thread.[1][4] Conversely, the memory order is called weak or relaxed when one thread cannot predict the order of operations arising from another thread.[1][4] Many naïvely written parallel algorithms fail when compiled or executed with a weak memory order.[5][6] The problem is most often solved by inserting memory barrier instructions into the program.[6][7]
In order to fully utilize the bandwidth of different types of memory such as caches and memory banks, few compilers or CPU architectures ensure perfectly strong ordering.[1][5] Among the commonly used architectures, x86-64 processors have the strongest memory order, but may still defer memory store instructions until after memory load instructions.[5][8] On the other end of the spectrum, DEC Alpha processors make practically no guarantees about memory order.[5]