Erlang Process doesn't receive messages

Wiger Ulf <>
Mon Jun 9 20:03:34 CEST 2003


The first time you use "receive Msg -> Msg ... end" and received 1,
the pattern describes an unbound variable Msg (which matches anything). It
is then bound to 1 (the first message). The next time you use the same
expression, Msg is already bound, and the expression becomes a pattern match
matching only the message 1. It still seems to work initially, since the
following message was also 1. When you try it with the message 2, it no
longer works, since 2 doesn't match the bound pattern.

You may try this:

f(Msg), receive Msg -> Msg after 3000 -> timeout end.

This tells the shell to "forget" the variable Msg before evaluating the
receive expression.

/Uffe

----- Original Message -----
From: "Serge Aleynikov" <>
To: <>
Sent: den 9 juni 2003 18:47
Subject: Erlang Process doesn't receive messages


> During the following test, I discovered a non-intuitive behavior that
> I'd appreciate to receive some help with.
>
> Given two processes: client (shown as a Erlang shell process), and
> server.  The server implements the following loop in a process separate
> from client:
>
> loop() ->
>      receive
>          stop ->
>              stopped;
>          {test, From, Arg} ->
>              timer:sleep(3000),
>              From ! Arg,
>              loop()
>      end.
>
> Perform the commands below from the client process shell.  What is not
> clear in this code is that despite the fact that the client has messages
> in its mailbox, the receive command times out without removing messages
> from the mailbox.
>
> Can anyone explain this?
>
> Thanks,
>
> Serge
>
> 94> Pid = server:start().
> <0.139.0>
> 95> process_info(self(), messages).
> {messages,[]}
> 96> Pid ! {test, self(), 1}.
> {test,<0.142.0>,1}
> 97> process_info(self(), messages).
> {messages,[]}
> 98> process_info(self(), messages).
> {messages,[1]}
> 99> receive Msg -> Msg after 3000 -> timeout end.
> 1
> 100> Pid ! {test, self(), 1}.
> {test,<0.142.0>,1}
> 101> Pid ! {test, self(), 2}.
> {test,<0.142.0>,2}
> 102> process_info(self(), messages).
> {messages,[1]}
> 103> process_info(self(), messages).
> {messages,[1]}
> 104> process_info(self(), messages).
> {messages,[1,2]}
> 105> receive Msg -> Msg after 3000 -> timeout end.
> 1
> 106> receive Msg -> Msg after 3000 -> timeout end.
> timeout
> 107> receive Msg -> Msg after 3000 -> timeout end.
> timeout
> 108> process_info(self(), messages).
> {messages,[2]}
> 109> os:type().
> {win32,nt}
> ------------------------
>




More information about the erlang-questions mailing list