[erlang-questions] On selective receive (Re: eep: multiple patterns)

Dominic Williams <>
Fri Jun 6 22:44:51 CEST 2008

Hello Richard,

Richard A. O'Keefe a écrit :

>> What if it would be possible to create Message Queues in addition to
>> process "default" Message Queue?
>> Where it would be possible to give options such as:
>> * limit to the amount message to store
>> [...]
> I'm beginning to sound like Little Johnny One-Note,
> but we can already get the effect of multiple message queues by
> having helper processes and giving out their PIDs instead.
 > [...]
>      counter(Self, Limit) when Limit > 0 ->
>          receive
>              {Self,Delta} -> counter(Self, Limit+Delta)
>            ; Msg          -> Self ! Msg,
>                              counter(Self, Limit-1)
>          end;
>      counter(Self, 0) ->
>          receive
>              {Self,Delta} -> counter(Self, Delta)
>          end.

You are quite right that this could be used to achieve the 
semantics of a separate (and bounded) message queue.

However, the original motivation was to avoid the occasional 
snowball effect that can result from selective receives, so 
I think using selective receive in the helper process 
defeats the purpose.

I wonder whether we could still use a helper process to 
regulate messages for  the handler process, but it would 
have to use the catch all receive. Maybe:

1) regulate by n° of messages per second (this can be done 
without communicating with the handler process)

2) use your counter idea, but something like this:

counter (Self, Limit) ->
   receive Message -> counter (Self, Limit, Message) end.

counter (Self, Limit, {Self, Delta}) ->
   counter (Self, Limit + Delta);
counter (Self, 0, _) ->
   counter (Self, 0);
counter (Self, Limit, Message) ->
   Self ! Message,
   counter (Self, Limit -1).

Hmmm. I'm tempted to actually try that out...


Dominic Williams


More information about the erlang-questions mailing list