threads and hashtable

G

Guest

Hi,
I have one thread adding objects to a hashtable, and a bunch of other
threads doing reads. MSDN says that multiple threads doing reads is safe.
My question, is if my write thread is only adding new buckets to the
hashtable, then there should not be a conflict w/ any other threads? Maybe
the add() method somehow modifies the general state of the hashtable during
the call, making it unsafe to read at that time. The bottom line is, does
anyone know if I have to lock the add() operation? I do not want to do this
if it is unnecessary.
Thanks in advance!
Yuri
 
N

Nick Hounsome

YuriL said:
Hi,
I have one thread adding objects to a hashtable, and a bunch of other
threads doing reads. MSDN says that multiple threads doing reads is safe.
My question, is if my write thread is only adding new buckets to the
hashtable, then there should not be a conflict w/ any other threads?

Why not?
I would expect adding a bucket to redistribute all objects in the hashtable.
Typically your access will be something like:

Bucket b = buckets[o.GetHashCode()%NumBuckets];
Maybe
the add() method somehow modifies the general state of the hashtable
during
the call, making it unsafe to read at that time. The bottom line is, does
anyone know if I have to lock the add() operation? I do not want to do
this
if it is unnecessary.
Thanks in advance!
Yuri

You are getting things backwards.
The rule for writing thread safe code is that unless the documentation
explicitly says that it is safe it isn't.

Even if it was somehow implemented in the way that you want in the current
implementation it why should it stay the same next time?

We've already seen someone post who thought that it was a bug because ah
hashcode in 2.0 was different to one in 1.1
 
N

Nick Hounsome

Vadym Stetsyak said:
Hello, YuriL!

Y> Hi,
Y> I have one thread adding objects to a hashtable, and a bunch of other
Y> threads doing reads. MSDN says that multiple threads doing reads is
Y> safe. My question, is if my write thread is only adding new buckets to
Y> the hashtable, then there should not be a conflict w/ any other
threads?
Y> Maybe the add() method somehow modifies the general state of the
Y> hashtable during the call, making it unsafe to read at that time. The
Y> bottom line is, does anyone know if I have to lock the add() operation?
Y> I do not want to do this if it is unnecessary.

Add operation will influence count property. This can hurt your reading
operation if it uses e.g. enumerator.
So, to be thread save add must be also threadsafe...

Take a look at Hashtable.Synchronized static method.

This is OK for adding and accessing but it wont make enumeration safe.
 
V

Vadym Stetsyak

Hello, Nick!

Yes, I agree with you. Synchronized method won't influence enumeration.
OTOH if we use have thread safe Hashtable ( obtained via Hashtable.Synchronized ),
we can write following code

Hastable hTbl; //our synchronized hashtable

lock(hTbl)
{
foreach(object o in hTbl.Keys)
{}
}
--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
 
M

Markus Stoeger

Vadym said:
Hello, Nick!

Yes, I agree with you. Synchronized method won't influence enumeration.
OTOH if we use have thread safe Hashtable ( obtained via Hashtable.Synchronized ),
we can write following code

Hastable hTbl; //our synchronized hashtable

lock(hTbl)

You have to lock on hTbl.SyncRoot.

hth,
Max
 

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