[erlang-questions] How exactly Erlang receive expression works?

Xavier Noria fxn@REDACTED
Fri Mar 16 15:57:04 CET 2018


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/29c2e120/attachment.htm>


More information about the erlang-questions mailing list