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

Threading Questions - Hashtables and Suspended threads

I'm programming a multi - threaded application using C#. I have two
questions regarding to the use of threads

1- I have a Hashtable that will have multiple writters and multiple readers.
I used the synchronized method to get a synchronized Hashtable. The
documentation says that enumerating is not a thread safe operation; even
using a synchronized Hashtable. The Hashtable has a "ContainsKey" method, is
this method thread safe? Does the "ContainsKey" method internally enumerate
the keys from the Hashtable in such a way that it needs the Hashtable to be
locked in order to be thread safe?

2. Threads have a Suspend method, which sleeps the thread until another
thread wakes it up using the Resume method. After the Suspend method is
called, the thread doesn't immeadiatelly go to the ThreadState.Suspended
state, it first passes through the ThreadState.SuspendRequested state, and
then it goes through the ThreadState.Suspended state. What will happen if
the resume method is called on a thread that is in the
ThreadState.SuspendRequested state? Will there be any problem?

Thanks for considering my questions
Jul 21 '05 #1
1 3523

"Carlos Kirkconnell" <ca*****@integrasofthn.com> wrote in message
news:%2******************@TK2MSFTNGP12.phx.gbl...
I'm programming a multi - threaded application using C#. I have two
questions regarding to the use of threads

1- I have a Hashtable that will have multiple writters and multiple readers. I used the synchronized method to get a synchronized Hashtable. The
documentation says that enumerating is not a thread safe operation; even
using a synchronized Hashtable. The Hashtable has a "ContainsKey" method, is this method thread safe? Does the "ContainsKey" method internally enumerate the keys from the Hashtable in such a way that it needs the Hashtable to be locked in order to be thread safe?
If an object or method is not specifically declared as threadsafe, it is
best to treat it as not threadsafe.

The documentation I've seen indicates that MSFT is trying to deprecate the
use of the synchronized method of the object itself; instead you should
synchronize access using your own private object. The fundamental reason is
that if you use their sync methods only a single call will be thread safe,
but multiple calls within a single logical block will not; the need to sync
around a block of operations requires you to use your own sync object.

2. Threads have a Suspend method, which sleeps the thread until another
thread wakes it up using the Resume method. After the Suspend method is
called, the thread doesn't immeadiatelly go to the ThreadState.Suspended
state, it first passes through the ThreadState.SuspendRequested state, and
then it goes through the ThreadState.Suspended state. What will happen if
the resume method is called on a thread that is in the
ThreadState.SuspendRequested state? Will there be any problem?

Thanks for considering my questions
From this question I will assume that you are considering using suspend and
resume to synchronize access to the hashtable, so here's my
opinion....don't. Use mutexes, reader-writer locks, or other synchronization
objects. Suspend/resume should only be used when you really do need to
suspend a thread, which normally is an exceedingly rare thing for an
application to do.

Jul 21 '05 #2

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

Similar topics

19
by: Jane Austine | last post by:
As far as I know python's threading module models after Java's. However, I can't find something equivalent to Java's interrupt and isInterrupted methods, along with InterruptedException....
11
by: Keith Langer | last post by:
I have an application which consists of a main work thread and multiple threads which each maintain a TCP socket. When a configuration change occurs, all activity on the socket threads must be...
77
by: Jon Skeet [C# MVP] | last post by:
Please excuse the cross-post - I'm pretty sure I've had interest in the article on all the groups this is posted to. I've finally managed to finish my article on multi-threading - at least for...
7
by: SharpCoderMP | last post by:
hi, i have a problem: i wrote a class that encapsulates communication with database. i wont go into details, but i wanted to use threads to avoid unresponsive ui so i did something more or less...
3
by: mjheitland | last post by:
Hi, I like to know how many threads are used by a Threading.Timer object. When I create a Threading.Timer object calling a short running method every 5 seconds I expected to have one additional...
1
by: Carlos Kirkconnell | last post by:
I'm programming a multi - threaded application using C#. I have two questions regarding to the use of threads 1- I have a Hashtable that will have multiple writters and multiple readers. I used...
9
by: russ.haley | last post by:
Threading and suspend I have an application that will load x number of "communicators". These "communicators" start up a communications channel with another application(of no importance). The...
15
by: WXS | last post by:
When I see things in .NET 2.0 like obsoletion of suspend/resume because of the public reason MS gives of they think people are using them inappropriately.. use mutex, monitor and other...
3
by: =?Utf-8?B?TWFyayBDaGFubmluZw==?= | last post by:
I have a code which registers all threads with a thread dump class. At intervals this thread dump class will dump the stack trace of all threads. As calling StackTrace(threadtoDump) from a...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: 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
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...

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.