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

Mats Cronqvist mats.cronqvist@REDACTED
Wed Feb 7 10:48:35 CET 2007


Hakan Mattsson wrote:
> 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> 
> 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.

   it's not safe if start/0 is supposed to return the pid of the server.
this is also not safe;

   my_service:start(),
   my_service ! hello.

   mats



More information about the erlang-questions mailing list