8.5 編寫健壯的應用程式-討論
阿新 • • 發佈:2018-12-31
loop(Processes) -> receive {From, {new_proc, Mod, Func, Args}} -> Id = spawn_link(Mod, Func, Args), From ! {keep_alive, started}, loop([{Id, Mod, Func, Args}|Processes]); {'EXIT', Id, _} -> case lists:keysearch(Id, 1, Processes) of false -> loop(Processes); {value, {Id, Mod, Func, Args}} -> P = lists:delete({Id,Mod,Func,Args}, Processes), Id1 = spawn_link(Mod, Func, Args), loop([{Id1, Mod, Func, Args} | P]) end end. new_process(Mod, Func, Args) -> keep_alive ! {self(), {new_proc, Mod, Func, Args}}, receive {keep_alive, started} -> true end.