Threading Questions - Hashtables and Suspended threads

  • Thread starter Carlos Kirkconnell
  • Start date
C

Carlos Kirkconnell

I'm programming a multi - threaded application using C#. I have two
questions regarding to the use of threads

1- I have a Hashtable that will have multiple writters and multiple readers.
I used the synchronized method to get a synchronized Hashtable. The
documentation says that enumerating is not a thread safe operation; even
using a synchronized Hashtable. The Hashtable has a "ContainsKey" method, is
this method thread safe? Does the "ContainsKey" method internally enumerate
the keys from the Hashtable in such a way that it needs the Hashtable to be
locked in order to be thread safe?

2. Threads have a Suspend method, which sleeps the thread until another
thread wakes it up using the Resume method. After the Suspend method is
called, the thread doesn't immeadiatelly go to the ThreadState.Suspended
state, it first passes through the ThreadState.SuspendRequested state, and
then it goes through the ThreadState.Suspended state. What will happen if
the resume method is called on a thread that is in the
ThreadState.SuspendRequested state? Will there be any problem?

Thanks for considering my questions
 
D

Dave

Carlos Kirkconnell said:
I'm programming a multi - threaded application using C#. I have two
questions regarding to the use of threads

1- I have a Hashtable that will have multiple writters and multiple readers.
I used the synchronized method to get a synchronized Hashtable. The
documentation says that enumerating is not a thread safe operation; even
using a synchronized Hashtable. The Hashtable has a "ContainsKey" method, is
this method thread safe? Does the "ContainsKey" method internally enumerate
the keys from the Hashtable in such a way that it needs the Hashtable to be
locked in order to be thread safe?
If an object or method is not specifically declared as threadsafe, it is
best to treat it as not threadsafe.

The documentation I've seen indicates that MSFT is trying to deprecate the
use of the synchronized method of the object itself; instead you should
synchronize access using your own private object. The fundamental reason is
that if you use their sync methods only a single call will be thread safe,
but multiple calls within a single logical block will not; the need to sync
around a block of operations requires you to use your own sync object.

2. Threads have a Suspend method, which sleeps the thread until another
thread wakes it up using the Resume method. After the Suspend method is
called, the thread doesn't immeadiatelly go to the ThreadState.Suspended
state, it first passes through the ThreadState.SuspendRequested state, and
then it goes through the ThreadState.Suspended state. What will happen if
the resume method is called on a thread that is in the
ThreadState.SuspendRequested state? Will there be any problem?

Thanks for considering my questions

From this question I will assume that you are considering using suspend and
resume to synchronize access to the hashtable, so here's my
opinion....don't. Use mutexes, reader-writer locks, or other synchronization
objects. Suspend/resume should only be used when you really do need to
suspend a thread, which normally is an exceedingly rare thing for an
application to do.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top