考虑下面的代码:
-module(add_two).
-compile(export_all).
start()->
process_flag(trap_exit, true),
Pid = spawn_link(add_two, loop, []),
register(add_two, Pid),
ok.
request()->
add_two ! bad,
receive
{'EXIT', _Pid, Reason} -> io:format("self:~w~n", [{error, Reason}]);
{Result} -> io:format("result:~w~n", [Result])
after
1000->timeout
end.
loop()->
receive
bad -> exit(nogoodreason_bad);
{request, Pid, Msg} -> Pid ! {result, Msg + 2}
end,
loop().
当我在 shell 中测试上面的代码时,我得到了两个不同输入顺序的不同结果,但是为什么呢?
第一个输入顺序:
Eshell V5.9.1 (abort with ^G)
1> add_two:request(ddd).
** exception error: undefined function add_two:request/1
2> add_two:start().
ok
3> add_two:request().
self:{error,nogoodreason_bad}
ok
第二个输入顺序:
Eshell V5.9.1 (abort with ^G)
1> add_two:start().
ok
2> add_two:request(ddd).
** exception error: undefined function add_two:request/1
3> add_two:request().
** exception error: bad argument
in function add_two:request/0 (add_two.erl, line 11)