Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / .NET SDK / November 2006

Tip: Looking for answers? Try searching our database.

hashtable & lock

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
David Thielen - 09 Nov 2006 16:58 GMT
Hi;

I have a hashtable that I am using in my codebehind in ASP.NET. In that
codebehind the hashtable is read, written to, and enumerated. Each entry in
the hashtable is tied to a session - the SessionId is the key for the entry.
So I will only have 1 thread writing to or reading from a specific entry in
the hashtable.

But there can be multiple threads (sessions) that all read/write/add/remove
the table at the same time. Reading the docs it looks like I need to put all
this access inside a lock() {...} or is there a better way?

Signature

thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

Cubicle Wars - http://www.windwardreports.com/film.htm

Chris Mullins - 09 Nov 2006 20:33 GMT
Using a lock is really the answer. So long as you need to
read/write/enumerate from any number of threads, you're stuck doing that.

In some use cases, the ReaderWriterLock works out better - it depends on the
ration of reads to the writes.

--
Chris Mullins, MCSD.NET, MCPD:Enterprise
http://www.coversant.net/blogs/cmullins

> Hi;
>
[quoted text clipped - 12 lines]
> all
> this access inside a lock() {...} or is there a better way?
"Peter Huang" [MSFT] - 10 Nov 2006 02:22 GMT
Hi Dave,

Yes, from the doc we will know that when multiple threads may read/write to
the HashTable in the same time, we need to do thread synchronization.

Thread Safety
Hashtable is thread safe for use by multiple reader threads, or a single
writing thread. It is not thread safe for multi-thread use, when any of the
threads perform write (update) operations. To support multiple writers all
operations on the Hashtable must be done through the wrapper returned by
the Synchronized method, provided that there are no threads reading the
Hashtable object.

Enumerating through a collection is intrinsically not a thread safe
procedure. Even when a collection is synchronized, other threads can 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.
Hashtable Class  
http://msdn2.microsoft.com/en-us/library/system.collections.hashtable.aspx

Hashtable.Synchronized Method
http://msdn2.microsoft.com/en-us/library/system.collections.hashtable.synchr
onized.aspx

In addition to the lock method, we have other approach thread
synchronization approach, but I think this one is easier.
e.g. ReaderWriterLock, AutoResetEvent and so on.

The goal to do thread synchronization is to prevent the scenario that
multiple threads access to a not thread safe object in the same time. In
this scenario, no writer thread and read thread access to the HashTable in
the same time.

Best regards,

Perter Huang
Microsoft Online Partner Support

Signature

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Ben Voigt - 10 Nov 2006 16:00 GMT
> Hi;
>
[quoted text clipped - 6 lines]
> in
> the hashtable.

Do you have some mechanism for preventing the user from loading two pages at
once?  Tabbed browsing makes this more than an accidental possibility.

> But there can be multiple threads (sessions) that all
> read/write/add/remove
> the table at the same time. Reading the docs it looks like I need to put
> all
> this access inside a lock() {...} or is there a better way?

What is TValue for your hashtable?  Arrange things so that you never
overwrite a hashtable entry, only the object pointed to.  Then, only adding
and removing entries needs a lock, and these only happen once per session.
Of course, you will also need to obtain the reference to your object under
lock, but only once per page.

Also, for scalability, use two-phase construction of table entry objects, so
that you can quickly add a blank element and then release the lock before
initializing it.
David Thielen - 10 Nov 2006 16:38 GMT
Yep, doing all that. However the reads have to be in a lock also as the
add/delete could be happening during the read.

Signature

thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

Cubicle Wars - http://www.windwardreports.com/film.htm

> > Hi;
> >
[quoted text clipped - 25 lines]
> that you can quickly add a blank element and then release the lock before
> initializing it.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.