ensure_started
Luke Gorrie
luke@REDACTED
Thu Feb 27 14:42:47 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.
This looks like code from fdoc. Sadly, I knew this was incorrect when
I wrote it, as is my ref()-less and monitor()-less 'call'
implementation when you get right down to it. Oops :-(
The trouble with registering in the server's init() function is that
then you need to synchronize the guy calling ensure_started() somehow,
since he needs to get the pid of the process that "wins the race." So
for "ensure_started" functions, I do like your second alternative with
the catch-and-retry.
Cheers,
Luke
More information about the erlang-questions
mailing list