[erlang-questions] How exactly Erlang receive expression works?
Athanasios Valsamakis
athanasios.valsamakis@REDACTED
Fri Mar 16 18:37:13 CET 2018
Mr. Xavier, you are absolutely right!
Thank you for pointing my error.
Is the following flowchart closer to the truth?
+-----------------+
| Enter receive. | ** Timer may have already been
elapsed
| Start timer | when entering this state.
+------+----------+
|
+-----v------+ +--------------------------------+
| is mailbox | Yes | suspend process until |timer
elapsed
+------> empty? +------------> a new message is put in
mailbox+-----------------+
| | | | or timer has elapsed** |
|
| +------------+ +-----+--------------------------+
+-------------v---------------------+ +-------------------+
| |No | |if
any messages have been put into | | evaluate |
| +------v----------------+ |new msg arrived |the
save queue, are reentered +-----> expressionsTimeout|
| | take the head message | | |into
the mailbox | | |
| | from the mailbox <--------+
+-----------------------------------+ +--------+----------+
| +------+----------------+
|
| |
+-----------------------------------+ +-------------+ |
| +-----v-------------+ +---------------+ |if any messages have
been put into +----->evaluate +---------->
| | match the message +---->remove message +----->the save queue, are
reentered | |expressions1 | |
| | against pattern1 |Yes |from mailbox | |into the mailbox
| +-------------+ |
| +-------------------+ +---------------+
+-----------------------------------+ |
| |No
|
| |
|
| +-----v-------------+ +---------------+
+-----------------------------------+ +-------------+ |
| | match the message +---->remove message +----->if any messages have
been put into +----->evaluate +---------->
| | against pattern2 |Yes |from mailbox | |the save queue, are
reentered | |expressions2 | |
| +-------------------+ +---------------+ |into the mailbox
| +-------------+ |
| |No
+-----------------------------------+ |
| |
|
| +-----v-------------+ +---------------+
+-----------------------------------+ +-------------+ |
| | match the message +---->remove message +----->if any messages have
been put into +----->evaluate +---------->
| | against pattern3 |Yes |from mailbox | |the save queue, are
reentered | |expressions3 | |
| +-------------------+ +---------------+ |into the mailbox
| +-------------+ |
| |No
+-----------------------------------+ |
| |
|
| +-----v------------------------------+
|
| | message is removed from the mailbox|
|
+------+ and put into a "save queue" |
|
| |
|
+------------------------------------+
|
|
+------------------+
|
| Exit receive.
<---------------------------------------------------------------------------------------------+
| Timer is cleared.|
+------------------+
On Fri, Mar 16, 2018 at 4:57 PM, Xavier Noria <fxn@REDACTED> wrote:
> I think there is a detail that does not square.
>
> Take this example[*]:
>
> -module(foo).
> -export([start/0]).
>
> start() ->
> self() ! matched_by_b,
> self() ! matched_by_a,
> a(),
> halt().
>
> a() ->
> receive
> matched_by_a ->
> io:format("matched by a/0~n"),
> b()
> end.
>
> b() ->
> receive
> matched_by_b ->
> io:format("matched by b/0~n")
> end.
>
>
> According to your (very nice!) diagram, this program should hang, because
> when b/0 is invoked, the message `matched_by_b` has already been moved into
> the save queue, and b/0 is called *while* the expression that matches
> `matched_by_a` is evaluated. The diagram says `matched_by_b` is not in the
> mailbox at that point.
>
> But the program terminates, ergo reductio ad absurdum :).
>
> I guess that diagram comes from the algorithm outlined on page 194 of the
> book. I believe you need to factor in step (5).
>
> Xavier
>
> [*] erlc foo.erl && erl -noshell -run foo
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20180316/5b02afda/attachment.htm>
-------------- next part --------------
+-----------------+
| Enter receive. | ** Timer may have already been elapsed
| Start timer | when entering this state.
+------+----------+
|
+-----v------+ +--------------------------------+
| is mailbox | Yes | suspend process until |timer elapsed
+------> empty? +------------> a new message is put in mailbox+-----------------+
| | | | or timer has elapsed** | |
| +------------+ +-----+--------------------------+ +-------------v---------------------+ +-------------------+
| |No | |if any messages have been put into | | evaluate |
| +------v----------------+ |new msg arrived |the save queue, are reentered +-----> expressionsTimeout|
| | take the head message | | |into the mailbox | | |
| | from the mailbox <--------+ +-----------------------------------+ +--------+----------+
| +------+----------------+ |
| | +-----------------------------------+ +-------------+ |
| +-----v-------------+ +---------------+ |if any messages have been put into +----->evaluate +---------->
| | match the message +---->remove message +----->the save queue, are reentered | |expressions1 | |
| | against pattern1 |Yes |from mailbox | |into the mailbox | +-------------+ |
| +-------------------+ +---------------+ +-----------------------------------+ |
| |No |
| | |
| +-----v-------------+ +---------------+ +-----------------------------------+ +-------------+ |
| | match the message +---->remove message +----->if any messages have been put into +----->evaluate +---------->
| | against pattern2 |Yes |from mailbox | |the save queue, are reentered | |expressions2 | |
| +-------------------+ +---------------+ |into the mailbox | +-------------+ |
| |No +-----------------------------------+ |
| | |
| +-----v-------------+ +---------------+ +-----------------------------------+ +-------------+ |
| | match the message +---->remove message +----->if any messages have been put into +----->evaluate +---------->
| | against pattern3 |Yes |from mailbox | |the save queue, are reentered | |expressions3 | |
| +-------------------+ +---------------+ |into the mailbox | +-------------+ |
| |No +-----------------------------------+ |
| | |
| +-----v------------------------------+ |
| | message is removed from the mailbox| |
+------+ and put into a "save queue" | |
| | |
+------------------------------------+ |
|
+------------------+ |
| Exit receive. <---------------------------------------------------------------------------------------------+
| Timer is cleared.|
+------------------+
More information about the erlang-questions
mailing list