ensure_started

Richard Carlsson <>
Thu Feb 27 10:50:53 CET 2003


Yes, I've found that this situation will typically happen every time you
write something like a test script that launches a number of clients;
then each of them will try to make sure the server is started, and since
calling 'register' seems to always cause the process to be swapped out,
all but one of them will fail in 'register'.

	/Richard

On Thu, 27 Feb 2003, Vlad Dumitrescu (EAW) wrote:

> Now I just had to implement this functionality and I noticed that the
> above isn't really correct. In fact, I even got a case during tests
> when the error showed up.
>
> start() ->
>     case whereis(?SERVER) of
> 	undefined ->
> 	    Pid = spawn(fun init/0),
> 	    %% some other process might have already registered it
> 	    %% since we last checked
> 	    case catch register(?SERVER, Pid) of
> 		true ->
> 		    %% we're the real thing
> 		    Pid;
> 		_ ->
> 		    %% other process got there first, use that one
> 		    Pid ! stop,
> 		    %% try again!
> 		    start()
> 	    end;
> 	Pid ->
> 	    Pid
>     end.
>
> I thought to share this, as it might cause confusion. Maybe it's not
> so often the scheduler will interrrupt start() just in the wrong
> place, but as I said, it happened to me (in relation with the debugger
> running).
>
> best regards,
> Vlad
>

Richard Carlsson ()   (This space intentionally left blank.)
E-mail: 	WWW: http://user.it.uu.se/~richardc/



More information about the erlang-questions mailing list