1. 程式人生 > >8.5 編寫健壯的應用程式-討論

8.5 編寫健壯的應用程式-討論

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.