[erlang-questions] Revisit - gen_server and priority queues

Eranga Udesh <>
Fri Jun 22 08:04:47 CEST 2007


Hi,

Revisiting below thread about priority based message retrieval, would it be
possible to extend gen_server (example) with,
	gen_server:pri_call/2/3
	gen_server:pri_cast/2
when run, it places the new message at the beginning of the message queue to
the gen_server process?

This doesn't preserve the FIFO for priority messages (but LIFO), but at
least it could give the facility to deliver a high priority message to a
process without waiting for its turn in the message queue.

Thanks,
- Eranga





-----Original Message-----
From: 
[mailto:] On Behalf Of Serge Aleynikov
Sent: Wednesday, October 26, 2005 12:02 AM
To: Rick Pettit; 
Cc: 
Subject: Re: gen_server and priority queues

Sorry I misunderstood Matthias' point.  Thanks for clarification, 
though, as I was also under impression that the order of pattern clauses 
mattered!

Serge

Rick Pettit wrote:
> On Tue, Oct 25, 2005 at 02:06:40PM -0400, Serge Aleynikov wrote:
> 
>>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.
> 
> 
> Actually, I believe it perfectly illustrates his point (which, btw, was
that
> the _low_ priority will be processed first, regardless of the pattern
clause
> order in the receive).
> 
> -Rick
> 
> 
>>------------------------------
>>     -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 Aleynikov
R&D Telecom, IDT Corp.
Tel: (973) 438-3436
Fax: (973) 438-1464





More information about the erlang-questions mailing list