erlang消息传递架构 [英] erlang message passing architecture
问题描述
- C! A发送的{非常大的邮件}
- C! B发送的{非常大的消息}
将C从A接收完整的消息,然后继续来自B的消息?或者是C可能会收到A的消息块以及B的消息块?
消息接收是一个原子操作。
如果您有兴趣如何完成,请阅读VM的源代码。如果我简化,发送过程就是执行以下步骤:
- 在发送过程中分配一个目标内存空间(称为环境)。
- 将邮件复制到该内存空间
- 在目标进程上执行外部锁定
- 链接消息进入邮箱链接列表
- 释放目标进程上的外部锁定
看到,复制是在外面(之前)关键部分完成的,关键部分是相当快的。这只是玩弄几个指针。
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?
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.
这篇关于erlang消息传递架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!