473,407 Members | 2,546 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,407 software developers and data experts.

Hashtable of Hashtables being accessed from multiple threads.

I have this Hashtable of Hashtables, and I'm accessing this object from
multiple threads, now the Hashtable object is thread safe for reading,
but not for writing, so I lock the object every time I need to write to
it, but now it occurred to me that maybe I could just lock one of the
Hashtables inside without locking the entire object, but then I thought
maybe some thread could instruct the outside Hashtable to remove an
inside Hashtable while it was being accessed by some other thread.

So now I'm confused!

Can i just lock an inside Hashtable when i'm trying to write to it, or
do i need to lock the entire object?

Oct 29 '06 #1
2 3133
If multiple threads will add/remove items from the parent ht, then you need
to use a lock. To make it simple, I would use 1 lock for any rw operations
on the collection set - unless you have a good reason not too and can verify
correctness of your other method.

--
William Stacey [C# MVP]

<PA******@gmail.comwrote in message
news:11*********************@i42g2000cwa.googlegro ups.com...
|I have this Hashtable of Hashtables, and I'm accessing this object from
| multiple threads, now the Hashtable object is thread safe for reading,
| but not for writing, so I lock the object every time I need to write to
| it, but now it occurred to me that maybe I could just lock one of the
| Hashtables inside without locking the entire object, but then I thought
| maybe some thread could instruct the outside Hashtable to remove an
| inside Hashtable while it was being accessed by some other thread.
|
| So now I'm confused!
|
| Can i just lock an inside Hashtable when i'm trying to write to it, or
| do i need to lock the entire object?
|
Oct 29 '06 #2

PA******@gmail.com wrote:
I have this Hashtable of Hashtables, and I'm accessing this object from
multiple threads, now the Hashtable object is thread safe for reading,
but not for writing, so I lock the object every time I need to write to
it, but now it occurred to me that maybe I could just lock one of the
Hashtables inside without locking the entire object, but then I thought
maybe some thread could instruct the outside Hashtable to remove an
inside Hashtable while it was being accessed by some other thread.

So now I'm confused!

Can i just lock an inside Hashtable when i'm trying to write to it, or
do i need to lock the entire object?
I'm no threading expert, but the little experience I have and my read
of the documentation tells me that you can't just lock on writing. You
have to lock on reading, too. Now I know that there are locking schemes
that allow multiple readers but that lock out readers while writing,
but I couldn't hope to tell you what they are.

The problem is that if you try to write to the Hashtable while another
thread is reading it, the reader may be hosed. Hashtables are
implemented as arrays (buckets) of linked lists. If I'm reading the
MSDN doc correctly, simultaneously writing and reading one of those
linked lists is not guaranteed threadsafe, so writers need to lock out
all readers while they're writing.

With regards to your question, well that all depends upon the semantics
you want to impose on your structure. Let's say, for example, that you
have three operations on the whole structure: read, insert, and remove.
Let's also say that if you remove the last entry from a sub-Hashtable
then you remove that sub-Hashtable, too. (If you were to just leave it
lying there, empty, then the following problem goes away....)

So, at the same moment you're inserting and item into a particular
sub-Hashtable, you're coincidentally removing (what was) the last item
from it. Will both operations succeed? Well, that depends upon their
order and it depends upon locking on the main Hashtable. Here are two
possible interleavings of events:

Thread 1: Fetch reference to correct sub-Hashtable
Thread 2: Fetch reference to correct sub-Hashtable
Thread 1: Lock sub-Hashtable for updates
Thread 1: Remove item from sub-Hashtable
Thread 1: Determine that sub-Hashtable is now empty
Thread 1: Remove sub-Hashtable from main Hashtable
Thread 1: Unlock sub-Hashtable
Thread 2: Lock sub-Hashtable for updates
Thread 2: Add item to sub-Hashtable
Thread 2: Unlock sub-Hashtable

or

Thread 1: Fetch reference to correct sub-Hashtable
Thread 2: Fetch reference to correct sub-Hashtable
Thread 2: Lock sub-Hashtable for updates
Thread 2: Add item to sub-Hashtable
Thread 2: Unlock sub-Hashtable for updates
Thread 1: Lock sub-Hashtable for updates
Thread 1: Remove item from sub-Hashtable
Thread 1: Determine that sub-Hashtable is not empty and so should not
be reclaimed
Thread 1: Unlock sub-Hashtable for updates

In the first interleaving, the item is correctly added to the
sub-Hashtable, but the Hashtable is floating in space and will be
reclaimed by the GC. In the second case the item is added to the
sub-Hashtable and it will stay in the structure. Maybe someone else
with better knowledge of thread synchronization can think of a clever
solution, but I don't see any alternative to locking the entire
structure so that multiple updates don't collide.

As for reads, it should be sufficient to lock only the part of the
structure that you're reading from: the main Hashtable long enough to
fetch the correct sub-table, and the sub-table while looking for the
element. This would assume that writes lock both the main table and the
appropraite sub-table during updates.

Oct 29 '06 #3

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

Similar topics

4
by: Anders Borum | last post by:
Hello! I have a list of singleton classes (model managers) that store objects internally using hashtables. Each of these classes use a single hashtable to store e.g. users, pages, elements and...
4
by: Matt C. | last post by:
I bet I know the answer already. I have a hashtable (hMaster) that holds several hashtables ("hTables") each of which holds other hashtables ("hColumns"). Presently, I am getting at the info I...
5
by: Cyrus | last post by:
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....
16
by: Sreekanth | last post by:
Hello, Is there any better collection than HashTable in terms of performance, when the type of the key is integer? Regards, Sreekanth.
8
by: Robin Tucker | last post by:
When I create a hashtable hashing on Object-->Item, can I mix "string" and "integer" as the key types? I have a single thumbnail cache for a database with (hashed on key) and a file view (hashed...
10
by: Ken Foster | last post by:
I have a hashtable keyed by some name, holding an instance of an object. Most of the time I use the hashtable in the traditional sense, given a name, I lookup the object, then I run a method on...
16
by: akantrowitz | last post by:
In csharp, what is the correct locking around reading and writing into a hashtable. Note that the reader is not looping through the keys, simply reading an item out with a specific key: If i...
8
by: chrisben | last post by:
Hi, If I am not sure how many items will be in my Hashtable/ArrayList dynamically (multiple threads can add/remove/check the item), however, I can estimate the total number will not exceed...
9
by: raylopez99 | last post by:
Hello all— I’m trying to get the below to work and cannot get the format right. It’s from this example: http://msdn.microsoft.com/en-us/library/8627sbea(VS.71).aspx What it is: I’m trying...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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.