This is a question about one writer and multiple simultaneous readers.
I expect this to ruffle some feathers and I'll probably get downvoted just for daring to ask this question, but I want to understand how it works. I know what mutexes and atomics are, no need to educate me on that.
Let's say I have a memory location accessible to multiple threads (a global variable, or a pointer I shared around.) It's the same size as the architecture, let's say its a single unsigned integer of 8 bytes size on a 64-bit system. It's set to 0
.
Let's assume I have a thousand threads reading it in a loop. If its 0
they do some important thing, and if its 1
they do another important thing, and if its neither 0
nor 1
they launch a nuclear missile.
Then one thread (exactly one, not multiple) overwrites this memory location with the value 1
. What happens?
See... my theory is that nothing bad happens and that this is okay. There is no data corruption. There is nothing half way in between a 0
and a 1
. One cycle it says 0
and the next it says 1
. No need for a mutex or an atomic. Am I right? And if not, why?
EDIT: The system asked me to explain why this is not the same as another question. The answer is because it's not the same as that question. If you're not sure how it's different, please read it again, particularly the parts that end with a question mark.