[erlang-questions] Singleton Process
Bernard Duggan
bernie@REDACTED
Fri Feb 6 00:59:20 CET 2009
Zvi wrote:
> Bernard Duggan-2 wrote:
>
>> Hi Edward,
>> Your code is a pretty reasonable attempt at a basic implementation (and
>> I don't mean that in any derogatory or rude sense - much of my code from
>> not very long ago follows a similar pattern).
>> The most obvious problem I can see with it is the race condition in
>> start() - if two processes call next_id() at the same time, they could
>> both call whereis(counter) and receive 'undefined' as the response -
>> they'd both then try to spawn and register the process,
>>
>
> Hi, Bernard
>
> what's your suggested workarround for this problem?
> I think the calls to whereis must be serialized in some way...
>
> Zvi
>
Hi Zvi,
The simple answer is, if you know you're going to need one and only
one, to explicitly start it at the beginning of your app, before you
start up anything that might use it, rather than trying to have it
lazily started on first use. Besides fixing the race condition, this
also simplifies the code a little. Remember that erlang processes are
very cheap, so the cost of starting up one a while before it actually
gets used is negligible.
Cheers,
Beranrd
More information about the erlang-questions
mailing list