[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.sdk

Use C# lock on Hashtable?

Siegfried Heintze

10/3/2002 4:33:00 AM

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?


4 Answers

Chad Myers

10/3/2002 4:53:00 AM

0


"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?

First, read the docs on Hashtable:
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemColl...
htableClassTopic.asp

(URL will wrap, be careful)

Note the section under "Thread Safety" where it talks about multiple
writiers.

To summarize,

Hashtable syncHashTable = Hashtable.Synchronized(new Hashtable());

// syncHashTable now supports multiple writiers and is thread safe

-c


Richard A. Lowe

10/3/2002 7:13:00 AM

0

If you did this:

Hashtable myCollection = Hashtable.Synchronized(new Hashtable());
lock( myCollection.SyncRoot ) {

}

then I would say it was redundant, but otherwise, even with one writer and
one reader, I don't believe you would, necessarily, be guaranteed to have
the same behaviour 100% of the time.

Others more knowledgeable than I may correct me, though.

Richard

"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?
>
>


Bryce Marshall

10/4/2002 1:12:00 PM

0

> 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?
>
>


Bryce Marshall

10/4/2002 1:20:00 PM

0

> If I have multiple writers, should I use the ReaderWriterLock?

I have found that the ReaderWriterLock offers considerably poorer
performance than the Monitor lock (i.e. the C# lock statement) when
synchronizing access to in-memory resources - even though the Monitor
effectively serializes all requests. The ReaderWriterLock I suspect is best
used to synchronize resource access that involves high I/O overhead.