Re: Synchronized Collection<T> Recommendation
- From: "Michael Primeaux" <mjprimeaux@xxxxxxx>
- Date: Sun, 30 Apr 2006 15:01:03 -0500
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
operations are complete. I mean, that's multi-threaded programming 101. I
can see an argument that assumes I hold a reference to an object (contained
in a collection) and that I set that reference equal to a new object
instance without first aquiring ownership of a synchronization primitive.
Effectively, I just updated the collection but without first aquiring a
lock. Again, you can't protect the developer from everything but then again
the developer had better know what they're doing. Ironically, this is the
very scenario that having a SyncRoot property addresses.
The intent of my original email was to inquire as to the recommended
solution for implementing a thread-safe collection that inherits from
Collection<T>. I certainly understand why the IsSynchronized property always
returns false and the SyncRoot property returns the current instance. I do
agree with this approach as the earnest is on the consumer to ensure
"proper" locking. I'm really just curious as to why no overridable members
exist for item retrieval on the Collection<T> class.
"Chris Chilvers" <keeper@xxxxxxxxxxx> wrote in message
news:ek1a52pd78ni0oe6mmic352smoskcvtmfm@xxxxxxxxxx
All this locking will however slow things down for such things as
for (int i = 0; i < list.Count; i++) ...
Which will now lock every time it reads something, and what if:
Thread A increments i to the last element (count-1)
Thread B deletes the last element
Thread A tries to read the last element
In these cases I'd favor having an unsyschronized collection and having
some specified way to obtain the collection exclusivly.
It just seems to me that syncronization on such things as a collection
are very much dependent upon what the collection is storing and what the
data is being used for. Without knowing the specific case it will be
used it just seems like one cannot reliably choose the locking mechanism
or guarantee that it will even work correctly as shown in the previous
example.
.
- References:
- Synchronized Collection<T> Recommendation
- From: Michael Primeaux
- Re: Synchronized Collection<T> Recommendation
- From: Chris Chilvers
- Re: Synchronized Collection<T> Recommendation
- From: Michael Primeaux
- Re: Synchronized Collection<T> Recommendation
- From: William Stacey [MVP]
- Re: Synchronized Collection<T> Recommendation
- From: Michael Primeaux
- Re: Synchronized Collection<T> Recommendation
- From: William Stacey [MVP]
- Re: Synchronized Collection<T> Recommendation
- From: Michael Primeaux
- Re: Synchronized Collection<T> Recommendation
- From: Chris Chilvers
- Synchronized Collection<T> Recommendation
- Prev by Date: Re: Error 500 on oXmlHTTP.Send
- Next by Date: Re: Synchronized Collection<T> Recommendation
- Previous by thread: Re: Synchronized Collection<T> Recommendation
- Next by thread: Re: Synchronized Collection<T> Recommendation
- Index(es):
Relevant Pages
|
Loading