当消息从 erlang 中的另一个进程获得权利时,如何在进程中打印消息?
How to print a message within a process when it gets the right from another process in erlang?
我是 erlang 的新手,我接到了这个任务:
- 编写函数 "setalarm(T,Message)" 启动两个进程的函数
同一时间。 T 毫秒后,第一个进程发送一条消息
到第二个进程,该消息将是 Message arg。
禁止使用函数库,只能使用原语(发送、接收、生成)
我作为新手写的代码比较有用,所以建议这样选择:
setalarm(T,Message)->
S = spawn(sotest,second,[]),
Pid = spawn(sotest,first,[S,T,Message]).
first(Pid,T,Message) ->
receive
after T -> Pid ! Message
end.
second() ->
receive
Message -> io:format("The message is ~p~n",[Message])
end.
我是 erlang 的新手,我接到了这个任务:
- 编写函数 "setalarm(T,Message)" 启动两个进程的函数 同一时间。 T 毫秒后,第一个进程发送一条消息 到第二个进程,该消息将是 Message arg。
禁止使用函数库,只能使用原语(发送、接收、生成)
我作为新手写的代码比较有用,所以建议这样选择:
setalarm(T,Message)->
S = spawn(sotest,second,[]),
Pid = spawn(sotest,first,[S,T,Message]).
first(Pid,T,Message) ->
receive
after T -> Pid ! Message
end.
second() ->
receive
Message -> io:format("The message is ~p~n",[Message])
end.