[erlang-questions] clarify: order of messages

Adam Lindberg adam@REDACTED
Mon Nov 19 09:07:33 CET 2007


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 <kosik@REDACTED> 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
> erlang-questions@REDACTED
> http://www.erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20071119/d5ab35f2/attachment.htm>


More information about the erlang-questions mailing list