Erlang:优先接收 [英] Erlang: priority receive

查看:131
本文介绍了Erlang:优先接收的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  prio() - > 
接收
{priority,X} - > 0 $之后的X
>
收到
X - > X
end
end。

我正在阅读一个名为 Priority Messaging Easy Easy ,其中描述了以下问题:


[code]示例[上]的主要问题是,我们不考虑当从内部阻止接收恢复评估时,我们可能在邮箱中有多个消息。在最糟糕的情况下,除了第一个潜在的巨大数量的元素之外,除了第一个可能是优先消息之外。在这种情况下,我们实际上已经完成了与我们打算做的相反的事情。


我没有完全得到这个。 / p>

问题(1):假设内部阻止接收将在 一个 之后被调用(即恢复) / strong>消息已到达消息队列,对吧?假设在短时间内从内部阻止接收恢复需要,现在还有一堆消息在队列中等待吗?



问题(2)另外,最坏的情况被描述为一个正常消息的队列,大量优先级消息。因为所有的接收子句首先被检查到队列中的第一个消息,而是针对队列中的第二个消息,...(source:this ,第69-70页)不应该是:很多正常消息在队列结尾处有一个优先级消息?

解决方案

Erlang是一个完全并发的语言,没有理由不能在同一时间发送多条消息。根据哦,这很快 - 其他线程在这个小时间内发生冲突的事情是不太可能的事情的假设是基本上是闭嘴你的眼睛,说没有这样的事情,比赛条件,有没有这样的事情,比赛条件...


Priority receive in Erlang can easily be implemented as follows:

prio() -> 
  receive 
    {priority, X} -> X 
  after 0 -> 
    receive 
      X -> X 
    end 
  end.

I am reading a paper called Priority Messaging made Easy in which they describe the following problem:

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.

I don't completely get this.

Question (1): I assume that the inner blocking receive will be 'called' (i.e. resumed) as soon as one message has arrived in the message queue, right? Is it realistic to assume that in the short time it takes to resume from the inner blocking receive, there would already be a whole bunch of messages waiting in the queue?

Question (2): Also, the worst case scenario is described as a queue with one normal message an a lot of priority messages. Since all the receive clauses are first checked against the first message in the queue, and then against the second message in the queue, ...(source: this book, page 69-70) shouldn't this be: a lot of normal messages with at the end of the queue a priority message?

解决方案

Erlang being a radically concurrent language, there's no reason you couldn't have been sent several messages at the exact same time. Making assumptions along the lines of "Oh, this is fast -- it's so unlikely other threads would do something that conflicts in that little time" is essentially the same thing as closing your eyes and saying "There's no such thing as race conditions, there's no such thing as race conditions..."

这篇关于Erlang:优先接收的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆