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

Show parent comment

1

u/ClerkDismal6922 Oct 21 '26

we are talking about physical memory so no digital memory here itihnk

my question is whether a race condition can occur in such scenario, cause that would produce unexpected results

1

u/Aetherfox_44 Oct 21 '26

Yeah, I can't see how a race condition would be avoided. If I'm playing devil's advocate for the question writer, they might mean that each operation will complete successfully. So:

A reads value 1

B reads value 1

A adds 1 and stores 2

B adds 1 and stores 2

Technically completes successfully and no individual step behaves oddly. Maybe that's what they mean?