[erlang-questions] Semantic change of receive statement? (Re: clarify: order of messages)

Roger Larsson <>
Mon Nov 19 09:32:28 CET 2007


Are you sure or are you guessing?
Have there been a change in semantics?

From "Concurrent Programming in ERLANG"
 Joe Armstrong, Robert Virding, Mike Williams
page 16
"receive is selective, that is to say, it takes the first message which
matches one of the message patterns from a queue of messages
waiting for the attention of the receiving process"

/RogerL

On måndag 19 november 2007, 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
> 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.
>
> Hope that helps. Here are some useful slides:
> http://www.duomark.com/erlang/briefings/euc2006/index.html
>
> Cheers!
> Adam
>
> On Nov 18, 2007 9:22 PM, Matej Kosik <> wrote:
> > -----BEGIN PGP SIGNED MESSAGE-----
> > Hash: SHA1
> >
> > KatolaZ wrote:
> > > On Sun, Nov 18, 2007 at 10:55:35AM -0800, Zvi wrote:
> > >> Same processes on the same node?
> > >> Will be this a case when processes are on different nodes?
> > >
> > > If the same process (only one) sends two messages one after the other
> > > (Msg1 and Ms2, respectively) to another process, then those two
> > > messages are going to be received in the same order as they have been
> > > sent, i.e. Msg1 first and then Msg2. And this does not depend on the
> > > fact that the two processes are running on different nodes (since I'm
> > > quite sure that message passing among remote nodes is handled using
> > > TCP sockets, so the *order* of transmission should be preserved...)
> > >
> > > Or, at least, I hope it works so ;-)
> >
> > I hope (but I am not sure) so too. My problems arise from the fact that I
> > did not studied internals
> > of Erlang VM (or I did not studied Erlang formal semantis---I am not sure
> > if there is something like
> > that).
> >
> > In a system I am in charge to write I have observed an anomaly. In this
> > case, there were at least
> > three processes.
> > - - server S
> > - - client C1
> > - - client C2
> >
> > The client C1 sends messages to server S "intensively". The client C2
> > sends messages to server S
> > occassionally. It is desirable that messages from C2 will be handled (in
> > a finite time). But I have
> > observed the opossite situation. As if the virtual machine did not
> > guaranteed the fairness. Only
> > messages from C1 were received by S. Is something like that possible?
> >
> > (to avoid this situation, I had to decrease the intensity by which client
> > C1 sends messages to
> > server S. Weird.).
> >
> > > HND
> > >
> > > Enzo
> >
> > - --
> > Matej Kosik
> > -----BEGIN PGP SIGNATURE-----
> > Version: GnuPG v1.4.6 (GNU/Linux)
> > Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
> >
> > iD8DBQFHQJ8aL+CaXfJI/hgRAuxdAJ9NBs/jCJDcrPB6pW6DEX+l7LUOQgCgg7u8
> > NAsPd9pNZ6OWMvCw9kufOjo=
> > =Zuq+
> > -----END PGP SIGNATURE-----
> > _______________________________________________
> > erlang-questions mailing list
> > 
> > http://www.erlang.org/mailman/listinfo/erlang-questions





More information about the erlang-questions mailing list