Re: Synchronized Collection<T> Recommendation



Hit SEND too soon.

That is true if I were to use lock / Monitor. I prefer to use the
ReaderWriterLock class in more cases (especially for the SyncRoot property)
for obvious reasons; I can't know (or presume to know) the usage profile of
the consumer.

The purpose of the SyncRoot property IS to leaverage the lock for other
scenarios outside of the collection but as related TO the collection.

I'll still keep my opinion; but then again, you know what those are like ;-)

"William Stacey [MVP]" <william.stacey@xxxxxxxxx> wrote in message
news:%23BbdJIHbGHA.4972@xxxxxxxxxxxxxxxxxxxxxxx
| 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
    ... Regarding the Thead A, B, A scenario below, ultimately it's the ... responsibility of the developer to ensure Thead A holds a lock until its ... you can't protect the developer from everything but then again ... returns false and the SyncRoot property returns the current instance. ...
    (microsoft.public.dotnet.languages.csharp)
  • 2.0 Collections data
    ... I suppose the answer would be yes as the 2.0 collections are type safe and ... data collection (protected by a lock based on the SyncRoot property of the ... from collection is also protected by a lock on SyncRoot property). ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: 2.0 Collections data
    ... This can be the Monitor class or a Mutex, ... In your case, if possible, you should only exclusively lock when you are ... data collection (protected by a lock based on the SyncRoot property of the ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Synchronized Collection Recommendation
    ... | find use for the SyncRoot and IsSynchronized properties in multi-operation ... | scenarios (i.e. add one item and remove another in the same protected ... 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)

Loading