Search code examples
multithreadingmemoryconcurrencycpumemory-barriers

Determine which store to memory that will happen first on a multi-core system?


Assuming I have two seperate threads, each running on their own CPU.

Both attempt to write to a shared piece of memory at the same time, how do I know which value will be stored into memory first/will get the most up-to-date verison?

Or will certain memory consistency models make sure this is impossible?

If so how?


Solution

  • "At the same time" does not really have meaning in multi-core systems. Do you mean the moment when the store instruction is executed (actually a span of time), the moment the value is put into the core-local store cache, the moment the store cache is flushed to the CPU-shared cache (note that there may be CPUs where a subset of cores has a shared cache), or when the cache is flushed to memory? Which core's view of the system do you want to use for observing the result? (In quad-core systems, it could well be that when core 1 and 2 both write values, cores 3 and 4 observe the writes in different orders.)

    CPUs have ways of ensuring that two writes have a guaranteed global order (memory barriers and interlocked load/store operations) - unless you use those, the results are unspecified and guaranteed to be surprising. And in the case of the C11 and C++11 memory model, they're fully undefined.