Erlang 中的优先接收可以很容易地实现如下:
prio() ->
receive
{priority, X} -> X
after 0 ->
receive
X -> X
end
end.
我正在阅读一篇名为 Priority Messaging made Easy 的论文,由 Nyström,他们在其中描述了以下问题:The main problem with the [code] example [above], is that we do not take into consideration that when evaluation is resumed from the inner blocking receive we may have more than one message in the mailbox. In a worst case scenario, all but the first, of potentially a huge number, of elements could be priority messages. In this scenario we would actually have accomplished the very opposite of what we intended to do.
我不完全明白这一点。
问题(1):我假设内部阻塞接收将在 后立即被“调用”(即恢复)。一 消息已经到达消息队列,对吗?假设在从内部阻塞接收恢复所需的短时间内,队列中已经有一大堆消息在等待是否现实?
问题 (2):此外,最坏的情况被描述为一个队列,其中包含一条普通消息和许多优先级消息。由于所有接收子句首先针对队列中的第一条消息进行检查,然后针对队列中的第二条消息进行检查,...(来源:this book,第 69-70 页)这不应该是:很多队列末尾有优先级消息的普通消息?
最佳答案
Erlang 是一种完全并发的语言,没有理由不能同时发送多条消息。沿着“哦,这很快——其他线程不太可能在那么短的时间内做一些冲突的事情”的假设与闭上眼睛说“没有竞争条件这样的事情,有没有比赛条件之类的东西……”
关于Erlang:优先接收,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/960016/