[erlang-questions] clarify: order of messages

Paulo Sérgio Almeida <>
Mon Nov 19 11:10:33 CET 2007


Hi,

Adam Lindberg wrote:
> A basic check to do is to look at your receive statement.
> 
> If it for example looks like this:
> 
> receive
>     {c1, Msg} ->
>         do_c1_stuff();
>     {c2, Msg} ->
>         do_c2_stuff()
> end
> 
> In this case if there are always some messages from C1 in the message 
> queue they will be picked first, regardless of the order. This is called 

This is not true. Messages are looked at in arrival order, and for each 
message the guards are matched in program order. If the only messages in 
the mailbox are either {c1, Msg} or {c2, Msg} the above behaves the same 
as the code below.

> selective receive. If you want to take the messages as they come you 
> should do something like this:
> 
> receive
>     {Sender, Msg} ->
>         case Sender of
>             c1 ->
>                 do_c1_stuff();
>             c2 ->
>                 do_c2_stuff()
>         end
> end
> 
> This way you will always pick a message, the first one to be in queue 
> (which matches pattern {A, B} of course) and _then_ look at the sender.

Regards,
/psa



More information about the erlang-questions mailing list