[erlang-questions] Erlang way for process-as-library?

Hakan Mattsson <>
Wed Feb 7 10:28:09 CET 2007

On Tue, 6 Feb 2007, Robert Virding wrote:

RV> Torbjorn Tornkvist wrote:
RV> > Either let the users of the library start it with
RV> > a start function or you could let every function
RV> > do it automatically. Example:
RV> > 
RV> > start() ->
RV> >   case whereis(my_service) of
RV> >     Pid when pid(Pid) -> Pid
RV> >     _ -> spawn(my_mod, my_server, [])
RV> >   end.
RV> That start function is not safe, especially on an SMP system. You could 
RV> get two rocesses running start simultaneously both not finding the 
RV> server and both trying to start it.

The start function is safe. Only one of the processes
will succeed in registering the name my_service.


More information about the erlang-questions mailing list