Erlang: getting priority

Erlang priority reception can be easily implemented as follows:

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

I am reading an article titled Priority Messages Made by Easy , which describes the following problem:

The main problem with the [code] example [above] is that we do not take into account that when the evaluation resumes with an internal lock, we can have more than one message in the mailbox. In the worst case, all messages except the first, potentially huge number of elements, can be priority messages. In this scenario, we would actually do exactly the opposite of what we intended to do.

I do not fully understand this.

Question (1): I assume that the internal blocking reception will be โ€œcalledโ€ (ie, resumed) as soon as one message has entered the message queue, right? Is it realistic to assume that in the short time it takes to resume from an internal blocking reception, a whole bunch of messages will already be in the queue?

Question (2): In addition, the worst case scenario is described as a queue with one normal message with a large number of priority messages. Since all receive offers are first checked for the first message in the queue, and then for the second message in the queue ... (source: this book , p. 69-70), should it be: there are a lot of normal messages at the end of which indicated priority message?

+6
erlang priority-queue
source share
4 answers

Erlang is a radically parallel language, there is no reason why you could not send multiple messages at the same time. Making assumptions on the lines "Oh, it's fast - it is so unlikely that other threads will do what conflicts in this short time", in fact, itโ€™s the same as turning a blind eye and saying "Thereโ€™s no such thing as race conditions , thereโ€™s no such thing as race conditions ... "

+5
source share

In (1): Whether this assumption can be made depends on the details of your situation. For example, all other processes may have waited for something to happen before sending you their messages.

On (2): In fact, it seems to me that the worst case will not have priority messages, since the mailbox must go through each time: "Still have a priority message?"

+4
source share

According to the erlang reference manual, the manual receives mailbox traffic in a temporary order. and blocks until a message matches one of the sentences.

Given that it sounds like the internal recording will be locked until it receives a message. Because of this, you can collect priority messages waiting for non-priority messages that are the opposite of what you want.

Too many ways out of this predicament throws a new after sentence in the inner reception. Or always match in the internal recipient.

Although viewing their function, the internal sentence should always be the same, but I assume that this is what they were talking about.

+1
source share

The statement you emphasize simply says that if you are in a blocking indoor receive unit, you can process a low priority message before a high priority message (because you all agree), which is not necessarily the intention.

This is a bit of an extreme case - I found that it is effective to process messages when you want some kind of filtering to be important. In other cases, I also monitored the depth of the process queue and changed my strategy accordingly. As an example of the latter, a simple process for generating a gen_server type (which used a cast to send a log message) could get quite a backup, since writing log messages to disk can be much slower than pushing messages to the process. If the queue depth is too great, I would refuse messages like information / spam, which I usually logged and processed only (write to disk) critical.

0
source share

All Articles