[erlang-questions] Message Receive Semantics (was eep: Multiple Patterns)

Sean Hinde <>
Sat May 31 11:49:41 CEST 2008


On 31 May 2008, at 06:10, Jay Nelson wrote:

> Sean Hinde wrote:
>
>> There is no contradiction here. In the first test the message queue
>> never grows so you do not get a slowdown.
>
> As Per's later posted code showed, the mailbox was loaded with
> messages before the consumers were started in both cases.  The
> problem has nothing to do with garbage collection, but with the
> linear scanning that takes place on the message queue.
>
>> Badly designed use of selective receive is one way to fill a message
>> queue, but by no means the only way. As I understand it once a
> message
>> has been examined and not matched it is not visited again until the
>> pattern changes.
>
> The message queue is inspected one message at a time.  All patterns
> are matched against that message.  If none match, the second message
> in the queue is matched.  Progress continues until a match is found or
> the end of the queue is hit.  If the end of the queue is hit and the
> receive
> doesn't timeout, then none of the previous messages are revisited.   
> Once
> a match happens, however, the scanning process goes back to the first
> message in the queue because the act of finding a match may have now
> made an earlier message match due to a change in pattern states.

Indeed, I posted my own followup agreeing that my understanding was  
wrong.

I still think that it would be an interesting optimisation - to store  
the last used pattern, and a pointer to where in the queue has been  
matched against that pattern - unless the pattern changes the system  
would not need to traverse the whole queue to re-test the same  
messages next time round the receive loop.

BR,
Sean




More information about the erlang-questions mailing list