Re: Synchronized Collection<T> Recommendation



| IMHO, I disagree with Microsoft's reasoning:
| http://blogs.msdn.com/brada/archive/2003/09/28/50391.aspx and do certainly
| find use for the SyncRoot and IsSynchronized properties in multi-operation
| scenarios (i.e. add one item and remove another in the same protected
| operation).

I have to agree with MS on this one. To add an item and remove in the same
protected operation means your taking the lock 3 times. Once for the
lock(SyncRoot), and once for each method internally. If you add a .Count in
there, then its four locks. True, you already own the lock, so the other
lock operations are faster, but it is still wasted overhead. As a user of
the collection, it often cleaner, and faster, to just use your own lock and
sync access to the collection yourself. Plus you may need to leverage that
lock for other issues outside of the collection.

--
William Stacey [MVP]



.



Relevant Pages

  • Re: Synchronized Collection Recommendation
    ... That is true if I were to use lock / Monitor. ... ReaderWriterLock class in more cases (especially for the SyncRoot property) ... scenarios outside of the collection but as related TO the collection. ... protected operation means your taking the lock 3 times. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Synchronized Collection Recommendation
    ... That is true if I were to use lock / Monitor. ... ReaderWriterLock class in more cases (especially for the SyncRoot property) ... protected operation means your taking the lock 3 times. ...
    (microsoft.public.dotnet.languages.csharp)
  • pthread_mutex_trylock vs. pthread_mutex_lock
    ... holds the lock. ... both scenarios are the same. ... puts thread on lock's 'waiting list' before going into suspend mode. ... The thread that owns the lock will boost priority of the thread from ...
    (comp.programming.threads)
  • Re: Recursive locks
    ... simple argument for why you should never need recursive locks: ... Only a function that might acquire a lock would care if ... that lock is recursive or not. ... There are many, many more scenarios. ...
    (comp.programming.threads)

Loading