[erlang-questions] receive doesn't work well in shell

June Kim juneaftn@REDACTED
Sat Apr 21 20:17:16 CEST 2007


It looks like using receive expression in the shell is valid and
proper. However, its behaviour is different from what I expected.

There is a module test1:

==================
-module(test1).
-compile(export_all).

get()->receive V->V end.
==================

Now we run an erlang shell and use the module:

==================
Erlang (BEAM) emulator version 5.5.3 [async-threads:0]

Eshell V5.5.3  (abort with ^G)
1> Me=self().
<0.29.0>
2> F=fun(Fun)-> receive V->Me!(V+1), Fun(Fun) end end.
#Fun<erl_eval.6.56006484>
3> P=spawn(fun()-> F(F) end).
<0.39.0>
4> P!3.
3
5> test1:get().
4
6> P!4.
4
7> test1:get().
5
8> P!5.
5
9> test1:get().
6

Everything looks alright.

Now we directly use receive expression in the shell, instead of
calling test1:get/0:

==================
Erlang (BEAM) emulator version 5.5.3 [async-threads:0]

Eshell V5.5.3  (abort with ^G)
1> Me=self().
<0.29.0>
2> F=fun(Fun)-> receive V->Me!(V+1), Fun(Fun) end end.
#Fun<erl_eval.6.56006484>
3> P=spawn(fun()-> F(F) end).
<0.33.0>
4> P!3.
3
5> receive V->V end.
4
6> P!4.
4
7> receive V->V end. %% it blocks here, where I expected "5" immediately.
==================

Why does the receive block on the input line 7? What's different to
calling test1:get/0?



More information about the erlang-questions mailing list