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

Thread safety in Hashtables remove method ..

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 enforce thread safety in a remove method?

Why would you care if two threads are trying to remove the same key from the
hashtable? In the end, the key is removed (and that was probably what you
were looking for).

Maybe this is caused by me not reading the following code correctly - and
not seing what it actually does is to make sure that no two threads are
removing keys, while other operations are performed on the hashtable (by
other threads).

public override void Remove(object key)
{
object local;

Monitor.Enter(local = _table.get_SyncRoot());
try
{
_table.Remove(key);
}
finally
{
Monitor.Exit(local);
}
}

--
venlig hilsen / with regards
anders borum
--
Nov 16 '05 #1
4 3324
> Why would you care if two threads are trying to remove the same key from the
hashtable? In the end, the key is removed (and that was probably what you
were looking for).
The problem is that the Hashtable can be grown by adding items while items are
being removed. These growth operations change the way the Hashtable works
such that if the Remove method init's it's search values for finding the key,
and then
the underlying collection changes, you'll get erroneous results.

If the method were to take a local reference of buckets, then even stranger
results
would occur. When the collection was grown, the key would be removed from the
previous collection, and not the grown collection. In effect the item wouldn't
be
removed.
--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers
Maybe this is caused by me not reading the following code correctly - and
not seing what it actually does is to make sure that no two threads are
removing keys, while other operations are performed on the hashtable (by
other threads).

public override void Remove(object key)
{
object local;

Monitor.Enter(local = _table.get_SyncRoot());
try
{
_table.Remove(key);
}
finally
{
Monitor.Exit(local);
}
}

--
venlig hilsen / with regards
anders borum
--

Nov 16 '05 #2
Hello!

So the locking process occurs on the entire instance of the instance,
effectively serializing all write (or change operations), so that only on
thread is able to change the instance at any given time?

Naturally all threads are allowed read operations.

Considering the following two methods, I would like to know if the locking
occurs on the instance (_table.get_SyncRoot()) or on the local object
variable (named "local").

public override void Add(object key, object value)
{
object local;
Monitor.Enter(local = _table.get_SyncRoot());
try
{
_table.Add(key, value);
}
finally
{
Monitor.Exit(local);
}
}

public override void Remove(object key)
{
object local;
Monitor.Enter(local = _table.get_SyncRoot());
try
{
_table.Remove(key);
}
finally
{
Monitor.Exit(local);
}
}

... isn't the above equal to (given that the entire instance is locked):

public override void Remove(object key)
{
object local;
lock(local = _table.get_SyncRoot());
{
_table.Remove(key);
}
}

or simply this:

public override void Remove(object key)
{
lock(_table.get_SyncRoot());
{
_table.Remove(key);
}
}

Thanks in advance. I am really currently reading up on threading and would
like some pointers to emphasize what I get from the books. The more I read,
the more I understand just how important thread safety is to e.g.
collections that serve as caches ..

--
venlig hilsen / with regards
anders borum
--
Nov 16 '05 #3
Anders Borum <a@b.dk> wrote:
So the locking process occurs on the entire instance of the instance,
effectively serializing all write (or change operations), so that only on
thread is able to change the instance at any given time?
Yes.
Naturally all threads are allowed read operations.
That's not necessarily "natural" - if a writing thread temporarily has
the table in an invalid state, reading from it would be a bad idea.
Considering the following two methods, I would like to know if the locking
occurs on the instance (_table.get_SyncRoot()) or on the local object
variable (named "local").
They're the same thing. The code below is exactly equivalent to

lock (_table.SyncRoot)
{
...
}

as you suggested (using the property syntax rather than calling the
get_SyncRoot method directly).
Thanks in advance. I am really currently reading up on threading and would
like some pointers to emphasize what I get from the books. The more I read,
the more I understand just how important thread safety is to e.g.
collections that serve as caches ..


See http://www.pobox.com/~skeet/csharp/threads/ for most of what I know
about threading.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
Hello!
That's not necessarily "natural" - if a writing thread temporarily has
the table in an invalid state, reading from it would be a bad idea.
Sorry; What I ment was, that from what I have seen in the Hashtable, reading
operations are not synchronized. Again, this is because the implementation
of the Hashtable allows for this. I agree with you that one could easily
think of situations where all operations on a instance are synchronized.
as you suggested (using the property syntax rather than calling the
get_SyncRoot method directly).
The code was taken directly from Salamanders C# decompiler. I am using the
SyncRoot property in my own code. Also thanks for clearing up the
lock(this); code.
See http://www.pobox.com/~skeet/csharp/threads/ for most of what I know
about threading.


I'll read that right away.

--
venlig hilsen / with regards
anders borum
--
Nov 16 '05 #5

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

Similar topics

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...
4
by: Jonathan Burd | last post by:
Greetings everyone, Here is a random string generator I wrote for an application and I'm wondering about the thread-safety of this function. I was told using static and global variables cause...
2
by: Frank Rizzo | last post by:
I tried it on dotnet.vb, but got no answers. I'll try it here now. I have an app that listen to data on a certain resource. Data can come in on this resource any time, so I have to listen for it...
1
by: Frank Rizzo | last post by:
Some of the classes in the framework are marked as thread-safe in the documentation. In particular the docs say the following: "Any public static (*Shared* in Visual Basic) members of this type...
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....
11
by: dee | last post by:
OleDbCommand class like many .NET classes has the following description in its help file: "Thread Safety Any public static (Shared in Visual Basic) members of this type are safe for...
17
by: Rainer Queck | last post by:
Hi NG, one more question about thread safety of generic lists. Let's assume a generic list: List<MyTyp> aList = new List<MyType>(); Would it be a problem if one thread removes elements from...
4
by: Warren Sirota | last post by:
Hi, I've got a method that I want to execute in a multithreaded environment (it's a specialized spider. I want to run a whole bunch of copies at low priority as a service). It works well running...
6
by: fniles | last post by:
I am using VB.NET 2003 and a socket control to receive and sending data to clients. As I receive data in 1 thread, I put it into an arraylist, and then I remove the data from arraylist and send it...
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: 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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...

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.