Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
516 views
in Technique[技术] by (71.8m points)

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

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 A
  • C ! {very large message} sent by B

Will C receive the complete message from A and then proceed for the message from B? or is it that C is likely be going to receive chunks of A's message along with chunks of B's message?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...