0

我想知道我所有生成的进程是否都处于空闲状态或不做任何事情。所以我有这个特殊的例子,它产生了 1000 个 erlang 进程proc/3

proc(A,B,C) ->
    receive
        {do} ->
            NewA = % doing something
            NewB = % doing something
            NewC = % doing something
            % doing something
            % ...
            io:format("Process: ~w is done doing something.", [self()]),
            proc(NewA,NewB,NewC)
    end

[<0.33.0>,<0.34.0>,<0.35.0>,<0.36.0>...<0.1032.0>]

现在这些进程中的每一个都会收到一条消息{do},告诉它们与其他进程同时执行其下的语句。我如何知道所有进程是否已完成执行,或者只是它们现在处于空闲状态?

4

1 回答 1

2

我遇到了这个问题,我通过将消息发回负责发送 do 消息的进程来解决它。就我而言,我只需要计算答案的数量。

proc(A,B,C,From) ->
    receive
        {do} ->
            NewA = % doing something
            NewB = % doing something
            NewC = % doing something
            % doing something
            % ...
            io:format("Process: ~w is done doing something.", [self()]),
            From ! {job_done,self()},
            proc(NewA,NewB,NewC,From)
    end
于 2013-10-14T12:39:26.063 回答