Communicate not share
Message passing avoids races by sending data through a channel rather than letting threads touch shared mutable state. Only one party owns a value at a time.
Why it is safer
When a value is sent it is handed off, not copied into a shared region:
- the sender gives up access after sending
- the receiver gains exclusive access on receipt
- no two threads hold a writable reference at once
This ownership handoff means there is no shared location for a race to occur on.
The cost and the tradeoff
Channels add copying or transfer overhead and can hide ordering bugs of their own, such as deadlock when two parties each wait to receive. But they replace fine grained locking with a clear ownership story.
Key idea
Message passing replaces shared mutable memory with channel handoffs, giving one owner at a time so there is no shared location for a data race to strike.