[erlang-questions] Imagine every (active) entity is a process
Mon Nov 5 16:32:55 CET 2007
> When I would want to start working on customer "23" for example, I
> will first need to check if customer "23" is already an active
> process. If yes, I get the Pid and exchange whatever messages I need
> with customer "23". If not, I create the process and proceed.
> Now, obviously, for my process to be known by all potential users of
> the process, I must register the process using the register(Name,Pid)
> function. As a name I could concatenate the entity name and its
> primary key (in my case: "customer_23").
> Isn't there a race condition here. Since I have to check first if a
> process exists before creating it, I might end up in a situation where
> two other processes are doing the same thing and creating twice a
> process for the same entity. The second would obviously fail and we
> could catch the error. But is this a correct way of proceeding? Also,
> is it possible to have thousands of registered processes (as opposed
> to non registered processes)?
You can serialize process lookup and creation
- yet another process, which is responsible for looking up "actual
and if necessary creating new process
- if you keep names private only way to access workers is through
name_resolver and no race any more.
- of course this creates new bottleneck
look at global http://erlang.org/doc/man/global.html
Just register() is only one building block. You need name lookup, new
and registration coordinated.
More information about the erlang-questions