【发布时间】:2015-04-19 15:43:50
【问题描述】:
我有以下代码:
-module(circle).
-export([proc/1,mother/0,chain/1]).
-spec mother() -> none().
mother() ->
register(mother,self()).
-spec proc(pid()) -> none().
proc(Next) when is_pid(Next) ->
receive
{_, Msg} -> Next ! {self(), Msg}
end.
-spec chain(integer()) -> pid().
chain(0) -> mother;
chain(N) when is_integer(N) ->
spawn(circle,proc,chain(N-1)).
它按预期编译,但是每当我运行时,当链到达0 参数时,它会抛出一个错误的参数错误。这是因为 erlang 将母亲视为原子,但是我之前调用了 mother 函数,该函数应该将 mother 注册为 pid。
我最初认为母亲在控制台中调用后没有注册:
-> circle:mother().
-> mother ! {abc}.
abc
我可以从这里推断出母亲确实被当作 Pid 对待。我怎样才能使代码工作?我怎样才能让二郎看到母亲是一个PID?
我想要实现的是在一个圆圈内构建N个流程。
【问题讨论】:
-
如果我的回答可以解决您的问题,您认为您可以发布异常中的堆栈跟踪吗?
标签: concurrency erlang