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?
erlang priority-queue
Rabarberski
source share