gen_server and priority queues

Matthias Lang matthias@REDACTED
Tue Oct 25 06:29:18 CEST 2005

Dominic wrote:
 > I was under the impression that:
 > receive
 >    {high, Msg} ->
 >       ...;
 >    {medium, Msg} ->
 >       ...;
 >    {low, Msg} ->
 >       ...;
 > end,
 > pulls higher priority messages out of the queue?

Before Marc Feeley set me straight at an EUC years ago, I carried the
same _incorrect_ impression around for a couple of years without ever
running into a situation where a program's behaviour was affected in a
way that mattered enough for me to notice reality.

For the practically minded:

     go() ->
       Pid = spawn_link(fun() -> rx() end),
       Pid ! low,
       Pid ! high,
       io:fwrite("both messages sent\n").
     rx() ->
         high -> io:fwrite("received high\n");
         low -> io:fwrite("received low\n")

For those who still doubt, plus language lawyers, the erlang reference
manual explains 'receive' in section 1.6.10:

   | Receives messages sent to the process using the send operator (!). The
   | patterns Pattern are sequentially matched against the first message in
   | time order in the mailbox, then the second, and so on.


More information about the erlang-questions mailing list