Re: Is Singleton collection of Singletons possible??

From: Daniel Billingsley (dbillingsley_at_NO.durcon.SPAAMM.com)
Date: 03/04/04


Date: Thu, 4 Mar 2004 16:02:37 -0500

Well, of course. But for thread-safeness it's the 3rd step that is of
significance. That is to say, setting aside my specific application, the
problem is two threads attempting to add the same ID. I take "A Hashtable
can support one writer and multiple readers concurrently. To support
multiple writers, all operations must be done through this wrapper only." to
mean the synchronized Hashtable itself wouldn't allow that, and I'd end up
with an ArgumentException. How do you take it? (That's assuming I use the
Add method which maybe is not the code you posted before as I recall.)

Now, that's not to say that using locking isn't better than dealing with the
exceptions, if that's where you'll go next. I'll have to evaluate that. I
just want to make sure I'm understanding these concepts correctly first
though.

"Jon Skeet [C# MVP]" <skeet@pobox.com> wrote in message
news:MPG.1aaef0a1db00259898a2dd@msnews.microsoft.com...
> Daniel Billingsley <dbillingsley@NO.durcon.SPAAMM.com> wrote:
>
> > My thinking is that since MS says the collections are thread-safe I
wouldn't
> > need to worry about the locking if GetCustomer().
>
> No, because you need to go through the process of:
>
> 1) Test if the customer is already there
> 2) If it is, return it
> 3) If not, create a new customer and put it in the collection
>
> Another thread could come in and request the same ID between steps 1
> and 3, meaning you end up with two customers for the same ID. Only each
> individual step is thread-safe without locking - you need to
> effectively do the whole operation atomically.
>
> --
> Jon Skeet - <skeet@pobox.com>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too