gen_server and priority queues

Serge Aleynikov serge@REDACTED
Tue Oct 25 20:06:40 CEST 2005


Matthias,

I actually run your example (modified to add the recursive call at the 
end), and got:

7> c(dominic).
{ok,dominic}
8> test1:go().
both messages sent
ok
received low
received high
exited
9>

I believe your point was that the high priority message will get process 
first, but this is not the case according to this printout.

Serge

------------------------------
      -module(dominic).
      -export([go/0]).

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

      rx() ->
        timer:sleep(1000),
        receive
          high -> io:fwrite("received high\n");
          low -> io:fwrite("received low\n")
        after 10000 ->
          io:fwrite("exited\n"),
          exit(normal)
        end,
        rx().
------------------------------

Matthias Lang wrote:
> 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:
> 
>      -module(dominic).
>      -export([go/0]).
>      
>      go() ->
>        Pid = spawn_link(fun() -> rx() end),
>        Pid ! low,
>        Pid ! high,
>        io:fwrite("both messages sent\n").
>      
>      rx() ->
>        timer:sleep(1000),
>        receive
>          high -> io:fwrite("received high\n");
>          low -> io:fwrite("received low\n")
>        end.
> 
> 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.
> 
>    http://www.erlang.se/doc/doc-5.4/pdf/reference_manual-5.4.pdf
> 
> Matthias
> 

-- 
Serge Aleynikov
R&D Telecom, IDT Corp.
Tel: (973) 438-3436
Fax: (973) 438-1464
serge@REDACTED



More information about the erlang-questions mailing list