ensure_started

Vlad Dumitrescu (EAW) <>
Thu Feb 27 16:35:32 CET 2003


> I posted the following suggestion a few weeks ago, now
> slightly modified:
> 
> ensure_started() ->
>    Id = {{?MODULE,ensure_started}, self()},
>    global:trans(
>        Id, fun() ->
>                case whereis(?MODULE) of
>                   undefined ->
>                      Pid = proc_lib:spawn(?MODULE,init,[]),
>                      register(?MODULE, Pid),
>                      {ok, Pid};
>                   Pid when pid(Pid) ->
>                      {ok, Pid}
>                end
>            end, [node()]).
> 
> 
> Since you know that the ensure_started() call is always
> local you can use global:trans(Id, Fun, [node()]) which will
> cut it down to two gen_server calls to the nearest global
> name server.

Yes, I know you did and I think this is the more general way to do it; I even tried this solution, but in this case I needed it to be as lightweight as possible in the normal case. The process I implemented is a "middle-man" between two already existing ones and a requirement was that the new process should be "invisible". I didn't want to risk that those two calls were too much...

regards,
Vlad



More information about the erlang-questions mailing list