3 ms·
Modern CPUs have a degree of memory inconsistency. If on Core#1: you do "A = 10, B = 12, A = 15", Core #2 might only see "B=12, A=15". However, the above is st
by dragontamer 8y ago
Modern CPUs have a degree of memory inconsistency. If on Core#1: you do "A = 10, B = 12, A = 15", Core #2 might only see "B=12, A=15".
However, the above is still a relatively strong case (like x86). On Power or ARM, you might see the ordering "A=15 THEN B=12". (The so called: Store-store reordering).
This happens because you write to cache first, and THEN the caches synchronize at a later time. So the caches can independently change their values. Every core has its own cache and therefore its own view of memory.
If you wanted the variables to be "assigned in the correct order", you have to do so called "barrier" or "memory-fence" instructions. The idea is, if the CPU's interconnect ever decides that its more efficient to order memory a certain way, then it will do it in the more efficient manner, rather than the "correct" order.
In most cases, such as single-threaded code, it doesn't make a difference at all. Only in multi-threaded code where mutexes and semaphores exist do problems occur.
A memory model is a programmer-invented abstraction to be able to reason about these cases. Everyone wants to use the faster and more-efficient CPUs, but its really, really, really complicated to think about right now. The paper presents a new memory model that they claim is easier to think about.