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

Jay Nelson <>
Sat May 31 07:10:07 CEST 2008

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  
 > 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  
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.

Check out the following tutorial on my website.  It discusses the issues
related to the message queue.  Ulf doesn't like my conclusions because
they don't encourage power users, but the vast majority of message
handling situations are not as sophisticated as what his group deals  


I seem to recall that Francesco's group had a presentation last year on
priority messaging, but don't go there -- priorities with erlang message
semantics is a very messy problem to deal with.  You are much better
off finding an alternative to priorities, unless you unload the whole
message queue and manage it in your own process as a real priority
queue rather than a linear list.


More information about the erlang-questions mailing list