gen_server and priority queues

Richard Cameron camster@REDACTED
Tue Oct 25 11:14:37 CEST 2005


On 24 Oct 2005, at 22:08, Dominic Williams wrote:

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

I thought that too, and it did take me a while to notice that  
priority queues were actually all explained in the erlang book  
<http://www.erlang.org/download/erlang-book-part1.pdf> on page 76.

Another gotcha (which I fell for too) was to write this:

handle(high, State) ->
     receive {high,Msg} ->
             do(Msg,State)
     after 0 ->
             handle(medium, State)
     end;
handle(medium, State) ->
     receive {medium,Msg} ->
             do(Msg, State)
     after 0 ->
             handle(low, State)
     end;
handle(low, State) ->
     receive {low,Msg} ->
             do(Msg, State)
     end.

... the difference being the last recieve is for {low,Msg} and not  
{_,Msg}. That can get itself into a horrible mess in which it ignores  
high or medium priority messages until at least one low priority one  
arrives in the inbox.

Richard.




More information about the erlang-questions mailing list