ensure_started
Bjorn Gustavsson
bjorn@REDACTED
Thu Feb 27 10:48:14 CET 2003
"Vlad Dumitrescu (EAW)" <Vlad.Dumitrescu@REDACTED> writes:
> Hi,
>
> I wonder about a thing. The following fun is an often used way to ensure that one uses "the one and only" server called ?MODULE.
>
> ensure_started() ->
> case whereis(?MODULE) of
> undefined ->
> Pid = proc_lib:spawn(?MODULE, init, []),
> register(?MODULE, Pid),
> {ok, Pid};
> Pid ->
> {ok, Pid}
> end.
>
A safer way is to move the call to register/2 into the init/0 function.
If two processes happens to start at almost the same time, the second
process will crash when calling register/2.
/Bjorn
--
Björn Gustavsson Ericsson Utvecklings AB
bjorn@REDACTED ÄT2/UAB/F/P
BOX 1505
+46 8 727 56 87 125 25 Älvsjö
More information about the erlang-questions
mailing list