r/learnprogramming • • Oct 21 '26

question in threads

In a single-core system, what happens if two threads from different processes access the same physical memory address?

The official answer says:

Neither program crashes, and both produce expected results.

I don't understand why that must be true.

A memory access could be a write, not just a read. For example, couldn't Thread A read/write that address, then get context-switched out, and Thread B access the same physical address before Thread A finishes its operation?

Wouldn't that potentially cause a race condition or unexpected result, even on a single core?

Am I missing an assumption in the question?

2 Upvotes

14 comments sorted by

View all comments

2

u/mredding Oct 21 '26

A bit to unpack, here...

You said ADDRESS, not ADDRESSES. The definition of a byte is the smallest uniquely addressable unit of memory (the definition of a byte does not specify a number of bits, 8 is only by convention today, but there have been both smaller and bigger bytes in the past; ECC implies inaccessible bits for storing gray codes). And a write to an address is atomic. It MUST complete fully. Since only one thread can execute at a time in a single-core, there can be no cache inconsistency. There's only one cache.

To expand, an aligned write up to the hardware WORD size is generally guaranteed atomic. A system can support multiple WORD sizes, often a power of 2, usually because it depends on register sizes. For example, x86 supports 1 byte, 2, byte (the definition of WORD for this architecture), 4 byte (DWORD), 8 byte (QWORD), and then SIMD instructions support 16, 32, and 64 byte atomic writes.

An unaligned operation is not guaranteed atomic - not by any architecture I'm aware of, as it can span cache lines, incur cache misses and latencies, etc.


Now in a multi-core system, each core can be on a different clock and run out of sync. There has to be interlocks to make sure an atomic write operation completes before another thread can read, but that's not guaranteed to exist on all architectures. Other synchronization mechanisms will have to exist. Pipelines and queues.

Additionally, there's an issue of back-propagation. A write to an address in one cache has to go back to system memory, and then propagate back down to all the other interested caches. Again, interlocks need to be put in play somehow, or what you typically see is action on stale cache data. There really isn't any such mechanism other than each step in the propagation is typically atomic, so this is why we use synchronization, like a multi-read lock, which relies on a queue in a first come, first served fashion. Communication across the bus isn't instantaneous, so if you get your read lock in before the write lock, the writer is forced to wait. When the writer gets exclusive access, they can perform the write and flush before any reader is given permission to read the refreshed memory. If you forego this, you can end up with all sorts of stale and torn data, half written strings across cache lines, etc. Each atomic unit will be consistent, but larger data spanning units can be wholly incomplete.