473,722 Members | 2,319 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3148
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******** *************@i 42g2000cwa.goog legroups.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
8350
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 so on (I have complete control of the objects stored). I am currently using a set of abstract cache classes that my model managers subclass. The hashtable in a model managers could, potentially, store more than 25.000 objects (let's imagine...
4
10385
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 want thusly (this compiles, at least): Hashtable hTable = (Hashtable)hMaster; Hashtable hColumn = (Hashtable)hTable; return hColumn.ContainsKey(codeValue);
5
15582
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. 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...
16
696
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
1689
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 on string). So, for example, when I want to know if the file "xyz.abc" is in the cache, I can write myTable.ContainsKey ( "xyz.abc" ) or if a given DB key is in the hash I can write myTable.ContainsKey ( 123 ). Or do the keys all have to be of...
10
15133
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 that object. Occaisionally however I need to run a method on every object in the hashtable. I've done both For..Each and iEnumeratior loops. The messy part is that under certain conditions, while iterating, a condition exists where I need to remove...
16
3463
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 have the following hashtable h which has multiple readers and 1 writer (on different threads) is this the correct locking below: lock (h.syncroot) {
8
3722
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 60000. How much can I gain in term of performance if I decalre the hash as Hashtable h = new Hashtable(60000); instead of
9
3115
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 to store multicast delegates in a hash table, and then fire the delegates one of two ways (after registering/ creating the delegates, etc).
0
8861
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9383
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9236
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9154
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8051
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6681
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4502
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3207
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2147
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.