[erlang-questions] clarify: order of messages

Andreas Hillqvist <>
Mon Nov 19 10:55:34 CET 2007


If you want to accomplish what you describe, Adam. You would have to
write it like:

loop() ->
  receive
    {c1, Msg} ->
      do_c1_stuff()
  after
    0 ->
      receive
        {c2, Msg} ->
          do_c2_stuff();
        {c1, Msg} ->
          do_c1_stuff();
        _ ->
          ok
    end,
    loop().

First receive statement will check the whole process message-box, if
necessary, to find a message from c1.

If there are no messages from c1 or the process message-box is empty.
The next receive statement will look for any c2 messages.

If there are no messages from c1, c2 or the message-box is empty it
will now block until a message is received.

So this loop will prioritize messages received from c1. But with out warranty.

If the message-box is flooded you will get a performance penalty.
So you will probably need some sort of flood control.


Regards
Andreas Hillqvist

2007/11/19, Adam Lindberg <>:
> 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
> >
>
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions
>



More information about the erlang-questions mailing list