Message receiving is an atomic operation.
If you are interested how it is done, read the source code of VM. If I simplify it, the sending process is doing those steps:
- Allocate a target memory space in the sending process (it's called environment).
- Copy the message to that memory space
- Take the external lock on the target process
- Link message into the mailbox linked list
- Release the external lock on the target process
As you can see, copying is done outside (before) critical section and the critical section is pretty fast. It is just juggling with few pointers.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…