Re: Is Singleton collection of Singletons possible??

From: Jon Skeet [C# MVP] (skeet_at_pobox.com)
Date: 03/04/04


Date: Thu, 4 Mar 2004 21:38:38 -0000

Daniel Billingsley <dbillingsley@NO.durcon.SPAAMM.com> wrote:
> 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.)

No, you wouldn't end up with an ArgumentException - you'd end up with
each call locking all other calls out until it completed.
 
> 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.

The problem is that if two different threads can both reach step 2, you
create more than one object for the same ID, which is usually a bad
move.

With threading, I always take the "simple is best" approach: while
you're dealing with shared data, take out a lock. I think people
generally believe that locks are more expensive than they really are.

-- 
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Relevant Pages

  • Re: using clustered index to optimize inserts ...
    ... I will try to explain locking in terms of Sybase docs... ... Allpages Locking: Allpages locking locks both data pages and index ... the data page is locked with an exclusive lock. ... Clustered Index: The datarows will be arranged as per the clustered ...
    (comp.databases.sybase)
  • Re: CSingleLock - known behaviour?
    ... It is better to design code that doesn't require locking. ... If you don't need the resource, don't lock it. ... magnitude less efficient, than locking once. ...
    (microsoft.public.vc.mfc)
  • Re: Strange multi-user timing phenomenon
    ... table record with pessimistic locking and both edit the same record. ... the recordset will be sufficient to lock out other users. ... update the recordset (if you had to edit data) and then close it. ...
    (microsoft.public.access.formscoding)
  • Re: Strange multi-user timing phenomenon
    ... I tried your locking method using a perssimistic recordset and it appears to ... user places his lock and grabs the same record. ... I would try opening a fixed single-row recordset with pessimistic locking, ...
    (microsoft.public.access.formscoding)
  • [UNIX] File Locking Local Denial of Service (Sendmails Impact)
    ... Any application which uses either flockor fcntlstyle locking or ... Since this attack requires a user to use their own account to lock a file, ... process holds an exclusive lock on a file, no other process can obtain an ... File locking is used throughout Sendmail for a variety of files including ...
    (Securiteam)