在Erlang中发送大消息时,是否有引发竞争条件的风险? [英] Do you risk triggering race conditions when sending large messages in Erlang?
问题描述
在Erlang中,如果两个进程A和B正在同时向进程C发送消息.会不会有比赛条件?
In Erlang if two processes A and B are sending message to a process C simultaneously. Will there be a race condition?
- C! A发送的{非常大的消息}
- C! B发送的{非常大的消息}
- C ! {very large message} sent by A
- C ! {very large message} sent by B
C会收到来自A的完整消息,然后继续处理来自B的消息吗?还是C可能会同时接收A的消息块和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?
推荐答案
消息接收是原子操作.
如果您对完成方法感兴趣,请阅读VM的源代码.如果简化一下,发送过程将执行以下步骤:
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:
- 在发送过程中分配目标内存空间(称为环境).
- 将消息复制到该存储空间
- 对目标进程进行外部锁定
- 将邮件链接到邮箱链接列表
- 释放对目标进程的外部锁定
如您所见,复制是在关键部分之外(之前)完成的,并且关键部分的速度非常快.只是在玩弄很少的指针.
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屋!