Do you risk triggering race conditions when sending large messages in Erlang?

后端 未结 1 1472
一生所求
一生所求 2020-12-21 04:40

In Erlang if two processes A and B are sending message to a process C simultaneously. Will there be a race condition?

  • C ! {very large message} sent by
相关标签:
1条回答
  • 2020-12-21 05:25

    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:

    1. Allocate a target memory space in the sending process (it's called environment).
    2. Copy the message to that memory space
    3. Take the external lock on the target process
    4. Link message into the mailbox linked list
    5. 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.

    0 讨论(0)
提交回复
热议问题