473,394 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Hashtable Synchronization to Ensure Thread Safety

I have a question regarding synchronization across multiple threads for a
Hashtable. Currently I have a Threadpool that is creating worker threads
based on requests to read/write to a hashtable. One function of the
Hashtable is to iterate through its keys, which apparently is inherently not
thread-safe. Other functions of the Hashtable include
adding/modifying/deleting.

To solve the synchronization issues I am doing two things:
1. Lock the syncroot of the Hashtable for any "write" operations:
Lock(myHashTable.Syncroot)
{ do some writing/deleting }

2. Wrap the Hashtable in a synchronized Hashtable for any iterations:
Hashtable mySynchedTable = Hashtable.Synchronized(myHashTable);
foreach(key in mySynchedTable)
{ .. do some reading }

My question is: is this enough? Is there anything missing, or should this be
enough to ensure synchronization across multiple threads accessing my
Hashtable.

Thanks!
Nov 16 '05 #1
5 15553
lock it yourself in all read or write methods using
lock(syncRoot)
{
foreach(...)
update/etc.
}

The problem with sync wrapper is it is locked every access so if you have a
method that does iteration and updates and counts, etc you keep locking it
multiple times. Also, something you need exclusive lock for a set of
operations that need to be in the same lock context to be safe. You could
still use the syncwrapper, but you end up locking twice. It is a bit more
work, but I would set your locks yourself unless your needs are simple.

--
William Stacey, MVP
http://mvp.support.microsoft.com

"Cyrus" <Cy***@discussions.microsoft.com> wrote in message
news:71**********************************@microsof t.com...
I have a question regarding synchronization across multiple threads for a
Hashtable. Currently I have a Threadpool that is creating worker threads
based on requests to read/write to a hashtable. One function of the
Hashtable is to iterate through its keys, which apparently is inherently not thread-safe. Other functions of the Hashtable include
adding/modifying/deleting.

To solve the synchronization issues I am doing two things:
1. Lock the syncroot of the Hashtable for any "write" operations:
Lock(myHashTable.Syncroot)
{ do some writing/deleting }

2. Wrap the Hashtable in a synchronized Hashtable for any iterations:
Hashtable mySynchedTable = Hashtable.Synchronized(myHashTable);
foreach(key in mySynchedTable)
{ .. do some reading }

My question is: is this enough? Is there anything missing, or should this be enough to ensure synchronization across multiple threads accessing my
Hashtable.

Thanks!


Nov 16 '05 #2
Thanks for the reply. Currently I have it set up to do: lock(myHash.syncRoot)
around any write methods and use a Hashtable.Synchronized wrapper around any
of my iterations like:

Hashtable mySynch = Hashtable.Synchronized(myHash);
foreach (string Key in mySynch.Keys)
{
MyObject tempObject = (MyObject)mySynch[Key];
tempObject.doSomeWork();
}

So from your post, I gather that this Synchronized Wrapper is doing a lock
each iteration of the Hashtable? So I'd be better off doing:
lock(myHash.syncRoot)
{
foreach (string Key in myHash.Keys)
{
MyObject tempObject = (MyObject)myHash[Key];
tempObject.doSomeWork();
}
}

so this would lock only once?

Thanks!


"William Stacey [MVP]" wrote:
lock it yourself in all read or write methods using
lock(syncRoot)
{
foreach(...)
update/etc.
}

The problem with sync wrapper is it is locked every access so if you have a
method that does iteration and updates and counts, etc you keep locking it
multiple times. Also, something you need exclusive lock for a set of
operations that need to be in the same lock context to be safe. You could
still use the syncwrapper, but you end up locking twice. It is a bit more
work, but I would set your locks yourself unless your needs are simple.

--
William Stacey, MVP
http://mvp.support.microsoft.com

"Cyrus" <Cy***@discussions.microsoft.com> wrote in message
news:71**********************************@microsof t.com...
I have a question regarding synchronization across multiple threads for a
Hashtable. Currently I have a Threadpool that is creating worker threads
based on requests to read/write to a hashtable. One function of the
Hashtable is to iterate through its keys, which apparently is inherently

not
thread-safe. Other functions of the Hashtable include
adding/modifying/deleting.

To solve the synchronization issues I am doing two things:
1. Lock the syncroot of the Hashtable for any "write" operations:
Lock(myHashTable.Syncroot)
{ do some writing/deleting }

2. Wrap the Hashtable in a synchronized Hashtable for any iterations:
Hashtable mySynchedTable = Hashtable.Synchronized(myHashTable);
foreach(key in mySynchedTable)
{ .. do some reading }

My question is: is this enough? Is there anything missing, or should this

be
enough to ensure synchronization across multiple threads accessing my
Hashtable.

Thanks!


Nov 16 '05 #3
> So from your post, I gather that this Synchronized Wrapper is doing a lock
each iteration of the Hashtable? Not 100% on that. Each method or property access is wrapped in a lock,
hence the name.
So I'd be better off doing:
lock(myHash.syncRoot)
{
foreach (string Key in myHash.Keys)
{
MyObject tempObject = (MyObject)myHash[Key];
tempObject.doSomeWork();
}
}


That is much better then first example. Also, from first example, you would
creating a new wrapper for every access - but can not tell for sure without
the whole class or a small class sample. Normally you end up needing a
lock{} to handle a few atomic operations regarding table and/or other stuff.
So having to handle both the wrapper and manual locks get a bit messy IMO
and have the two lock overhead if using both. For that reason I find easier
to reason about the locking on shared resources if I just do the locks
myself and I think makes your code more understandable as it is explicit
what your doing. But others may disagree. Cheers.

--
William Stacey, MVP
http://mvp.support.microsoft.com

Nov 16 '05 #4
Thanks! I'll give it a shot!

"William Stacey [MVP]" wrote:
So from your post, I gather that this Synchronized Wrapper is doing a lock
each iteration of the Hashtable?

Not 100% on that. Each method or property access is wrapped in a lock,
hence the name.
So I'd be better off doing:
lock(myHash.syncRoot)
{
foreach (string Key in myHash.Keys)
{
MyObject tempObject = (MyObject)myHash[Key];
tempObject.doSomeWork();
}
}


That is much better then first example. Also, from first example, you would
creating a new wrapper for every access - but can not tell for sure without
the whole class or a small class sample. Normally you end up needing a
lock{} to handle a few atomic operations regarding table and/or other stuff.
So having to handle both the wrapper and manual locks get a bit messy IMO
and have the two lock overhead if using both. For that reason I find easier
to reason about the locking on shared resources if I just do the locks
myself and I think makes your code more understandable as it is explicit
what your doing. But others may disagree. Cheers.

--
William Stacey, MVP
http://mvp.support.microsoft.com

Nov 16 '05 #5
WXS
Actually in .NET 1.1. the synchronized hashtable only actually locks on
removes if I recall correctly.
Some interesting info for people using hashtables:
1. When threading use the synchronized hastable wrapper if you are not
worried about not seeing writes immediately when done from another thread.
2. The synchronized hashtable only locks on deletions internally as it
copies buckets normal to avoid locking
3. Given the above implementation it is slow, but provides good parallelism
and reduced contention
4. Given #1 is ok for you, you only need to lock if you need to enumerate
all of the elements in the hashtable since the enmerator is not thread safe.
5. For even higher performance given a well known dataset and using strings
as keys you might try a different hash algorithm, the MS one is nicely
distributed producing few collisions but is slow compared to other algorithms
that would produce more collisions for large data sets, but if your data set
is well known or small and diverse some more simplistic algorithms may work
for you.
"Cyrus" wrote:
Thanks! I'll give it a shot!

"William Stacey [MVP]" wrote:
So from your post, I gather that this Synchronized Wrapper is doing a lock
each iteration of the Hashtable?

Not 100% on that. Each method or property access is wrapped in a lock,
hence the name.
So I'd be better off doing:
lock(myHash.syncRoot)
{
foreach (string Key in myHash.Keys)
{
MyObject tempObject = (MyObject)myHash[Key];
tempObject.doSomeWork();
}
}


That is much better then first example. Also, from first example, you would
creating a new wrapper for every access - but can not tell for sure without
the whole class or a small class sample. Normally you end up needing a
lock{} to handle a few atomic operations regarding table and/or other stuff.
So having to handle both the wrapper and manual locks get a bit messy IMO
and have the two lock overhead if using both. For that reason I find easier
to reason about the locking on shared resources if I just do the locks
myself and I think makes your code more understandable as it is explicit
what your doing. But others may disagree. Cheers.

--
William Stacey, MVP
http://mvp.support.microsoft.com

Mar 14 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Philip V Pham | last post by:
These questions apply to std vector, map, and cout: I am uncertain of the thread safety for reading/writing for std templates. I know if all threads are reading concurrently, it is thread...
4
by: Anders Borum | last post by:
Hello! I'm am currently working on making a central cache component threadsafe, and was looking at the synchronized implementation of the Hashtable. I was wondering why you'd really want to...
3
by: GroZZleR | last post by:
Hey all, I'm threading newbie, I've never attempted it before. I've got a button that when clicked starts up a thread to process some files. This keeps my GUI responsive and gets the tasks...
5
by: Keith Langer | last post by:
I have a hashtable which is accessed by two threads. One thread does all writing and enumeration in the hashtable, and the other thread has read-only access to the table directly through keys but...
5
by: jzlondon | last post by:
Hi, I have a question that I wonder if someone might be able to help me with... I have an application which handles real-time financial data from a third party source. The data comes in via...
4
by: news.microsoft.com | last post by:
Hi all, Assume we have two entity class. Class1: Name: House Property: ID:int Name:String Desktops:Desktop
13
by: arun.darra | last post by:
Are the following thread safe: 1. Assuming Object is any simple object Object* fn() { Object *p = new Object(); return p; } 2. is return by value thread safe?
6
by: Olumide | last post by:
Hi - I've got a class that contains static member functions alone, all of whose arguments are passed by reference as shown below: class MySpiffyClass{ // no constructor, destructor or...
13
by: Henri.Chinasque | last post by:
Hi all, I am wondering about thread safety and member variables. If I have such a class: class foo { private float m_floater = 0.0; public void bar(){ m_floater = true; }
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.