Home All Groups Group Topic Archive Search About

threads and hashtable

Author
24 Feb 2006 3:36 PM
YuriL
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

Author
24 Feb 2006 3:41 PM
Vadym Stetsyak
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.

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
Author
24 Feb 2006 3:54 PM
Nick Hounsome
Show quote
"Vadym Stetsyak" <vady***@ukr.net> wrote in message
news:%23Z7zXjVOGHA.1312@TK2MSFTNGP09.phx.gbl...
> 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.
Author
24 Feb 2006 4:23 PM
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
Author
24 Feb 2006 4:38 PM
Markus Stoeger
Vadym Stetsyak wrote:
> 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
Author
24 Feb 2006 5:08 PM
Vadym Stetsyak
Hello, Markus!

Agreed

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
Author
27 Feb 2006 2:05 PM
YuriL
Thank you everyone for the feedback.
Yuri

Show quote
"Vadym Stetsyak" wrote:

> Hello, Markus!
>
> Agreed
>
> --
> Regards, Vadym Stetsyak
> www: http://vadmyst.blogspot
Author
24 Feb 2006 3:46 PM
Nick Hounsome
"YuriL" <Yu***@discussions.microsoft.com> wrote in message
news:92B7229B-1223-4FE7-93A3-2A587203079F@microsoft.com...
> 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

AddThis Social Bookmark Button