Bryce Marshall
10/4/2002 1:12:00 PM
> If I have a single writer and multiple readers, do I need to use the
> following code (assuming that no one is ever enumerating the hashtable)?
> Hashtable myCollection = new Hashtable();
> lock( myCollection.SyncRoot ) {
The Hashtable is supposed to support multiple readers and a single writer
(although I have heard rumours that in some non-intel multi-processor
environments that this is not the case!).
To this end, if you have a single writer, then you are safe.
To support multiple writers, you may synchronize any access to any methods
that attempt to write to the hashtable - example:
public void DoInsert(object key, object value)
{
lock(this) {
hashtable.Add(key, value);
}
}
NOTE: The SyncRoot property does not return a syncronized wrapper (it just
returns a reference to the same hashtable) - it is there for classes that
derive from Hashtable to override. To obtain a synchronized wrapper, you
must call the static Synchronized() method.
Regards,
Bryce Marshall
"Siegfried Heintze" <siegfried@heintze.com> wrote in message
news:upnel773fbrfca@corp.supernews.com...
> Since I did not get a response to my other query, let me simplify the
> question:
>
> If I have a single writer and multiple readers, do I need to use the
> following code (assuming that no one is ever enumerating the hashtable)?
>
> Hashtable myCollection = new Hashtable();
> lock( myCollection.SyncRoot ) {
>
> }
>
> I believe this is superfluous.
>
> If I have multiple writers, should I use the ReaderWriterLock?
>
>