473,378 Members | 1,623 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,378 software developers and data experts.

thread safety of hashtable

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 never with
enumerators. Is there any reason to worry about synclocks?

thanks,
Keith
Jul 21 '05 #1
5 1757
Hi Keith,

Sure there is to worry.
This is what help says on this topic:
"To support one or more writers, all operations on the Hashtable must be
done through the wrapper returned by the Synchronized method.

Enumerating through a collection is intrinsically not a thread-safe
procedure. Even when a collection is synchronized, other threads could still
modify the collection, which causes the enumerator to throw an exception. To
guarantee thread safety during enumeration, you can either lock the
collection during the entire enumeration or catch the exceptions resulting
from changes made by other threads."

I hope it is clear enough (use Hashtable returned by Synchronized method +
lock it when doing enumeration)
--
Miha Markic - RightHand .NET consulting & development
miha at rthand com
www.rhand.com

"Keith Langer" <ta******@aol.com> wrote in message
news:15**************************@posting.google.c om...
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 never with
enumerators. Is there any reason to worry about synclocks?

thanks,
Keith

Jul 21 '05 #2
Miha,

If you re-read my original post, you'll see that I only have one
writer thread, and it is also the only thread doing enumeration. The
other thread has read only access to the hashtable and does not use
enumeration. I don't believe that I'm meeting either criteria for
requiring synchronized access.

Keith

"Miha Markic" <miha at rthand com> wrote in message news:<#M*************@tk2msftngp13.phx.gbl>...
Hi Keith,

Sure there is to worry.
This is what help says on this topic:
"To support one or more writers, all operations on the Hashtable must be
done through the wrapper returned by the Synchronized method.

Enumerating through a collection is intrinsically not a thread-safe
procedure. Even when a collection is synchronized, other threads could still
modify the collection, which causes the enumerator to throw an exception. To
guarantee thread safety during enumeration, you can either lock the
collection during the entire enumeration or catch the exceptions resulting
from changes made by other threads."

I hope it is clear enough (use Hashtable returned by Synchronized method +
lock it when doing enumeration)
--
Miha Markic - RightHand .NET consulting & development
miha at rthand com
www.rhand.com

"Keith Langer" <ta******@aol.com> wrote in message
news:15**************************@posting.google.c om...
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 never with
enumerators. Is there any reason to worry about synclocks?

thanks,
Keith

Jul 21 '05 #3
"Keith Langer" <ta******@aol.com> wrote in message
news:15*************************@posting.google.co m...
Miha,

If you re-read my original post, you'll see that I only have one
writer thread, and it is also the only thread doing enumeration. The
other thread has read only access to the hashtable and does not use
enumeration. I don't believe that I'm meeting either criteria for
requiring synchronized access.


Keith,

This would be true if writting into Hashtable is an atomic operation - which
it isn't.
What happens if writer thread is switched to reader thread in the middle of
writing operation to hashtable?

--
Miha Markic - RightHand .NET consulting & development
miha at rthand com
www.rhand.com
Jul 21 '05 #4
Miha,

If writer switches to reader, then nothing could be writing, so it
wouldn't matter. The real question is can I have one writer and one
reader (which does not enumerate). Could you explain what non-atomic
operations occur when writing to or reading from a hashtable?

Keith
"Miha Markic" <miha at rthand com> wrote in message news:<Ow**************@TK2MSFTNGP10.phx.gbl>...
"Keith Langer" <ta******@aol.com> wrote in message
news:15*************************@posting.google.co m...
Miha,

If you re-read my original post, you'll see that I only have one
writer thread, and it is also the only thread doing enumeration. The
other thread has read only access to the hashtable and does not use
enumeration. I don't believe that I'm meeting either criteria for
requiring synchronized access.


Keith,

This would be true if writting into Hashtable is an atomic operation - which
it isn't.
What happens if writer thread is switched to reader thread in the middle of
writing operation to hashtable?

Jul 21 '05 #5
Hi Keith,

"Keith Langer" <ta******@aol.com> wrote in message
news:15**************************@posting.google.c om...
Miha,

If writer switches to reader, then nothing could be writing, so it
wouldn't matter.
I am talking about two threads - one writer and one reader.

The real question is can I have one writer and one reader (which does not enumerate). Could you explain what non-atomic
operations occur when writing to or reading from a hashtable?


An atomic opertaion is the operation that can't be interupted.
As an example, assigning an integer is an atomic operation because it is
done in one procesor operation (at machine level).
So, it can't be interrupted by thread scheduler.
Assigning a string is not an atomic operation because it involves more than
one procesor's operation (allocating memory, copying chars, etc).
It *can* be interrupted by thread scheduler in the middle of assignment -
when part of the job is done and part not.
So, if you gonna read same string in the middle of assignment operation from
within another thread it will cause havoc because string will be in possibly
invalid state.
The same is true for hashtables - adding items is not an atomic operation.

HTH
--
Miha Markic - RightHand .NET consulting & development
miha at rthand com
www.rhand.com
Jul 21 '05 #6

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

Similar topics

2
by: David | last post by:
Hi guys I have in my application many collections (HashTable and ArrayList)loaded with configuration data, that means they are loaded at startup and then they are accessed only for read-only...
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...
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...
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....
2
by: Jack | last post by:
How does dotnet handles thread safety of Collections ? I derived some classes from CollectionBase and DictionaryBase, and created some typed Add, Remove methods to call typeless based Add, Remove...
0
by: Allan Ebdrup | last post by:
I'm inside a function where I have a static cache, when the cache needs to be updated I want to do it asyncronously, because updating the cache takes a while. I want to use thread safety when...
11
by: Eric | last post by:
I have a VB.net dll project with a class that is a singleton. I've been using this in winform apps without any problems. I would like to use this same dll in a web form project but my singleton...
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...
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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
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: 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...

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.