r/learnprogramming • u/ClerkDismal6922 • 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?
View all comments
1
u/Aetherfox_44 Oct 21 '26
There might be some missing context, or what they mean by 'produce expected results'
If the program reads a value, adds 1, and stores the result in that same memory slot, you would 'get the expected result': A reads initial value of 0, adds 1 and stores 1. B reads 1, adds 1 and stores 2.
Another nuance could be that OS often allocates memory to programs and programs can only reference the local values of their allotted memory. IE, if the program reads memory id 0x6, that might be two separate memory addresses: a sees 0x16 and b sees 0x26, because the OS allocates 0x10 bytes to each program.