Spawning processes

Vlad Dumitrescu (EAW) Vlad.Dumitrescu@REDACTED
Fri Feb 14 15:24:12 CET 2003


> Pid = case whereis(Name) of
>          undefined ->
>             global:trans({{start,Name},self()},
>                          fun() ->
>                             start()
>                          end);
>          P when pid(P) ->
>             P
>       end.

Thank you, Ulf. That's a good suggestion! My solution was the commented-out start() below, which works fine when the consecutive calls are from the same process (which was my problem), but of course it can't protect in case of two processes calling at the same time. I only hope it won't be too much of a performance hit (most of the time, the server exists)...

best regards,
Vlad

start() ->
    global:trans({{start, ?SERVER}, self()}, fun start_1/0, [node()]).

%start() ->
%    timer:sleep(0),
%    start_1().

start_1() ->
    case whereis(?SERVER) of
	undefined ->
	    spawn(fun init/0);
	Pid ->
	    Pid
    end.



More information about the erlang-questions mailing list