[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.sdk

Thread Safety and Collections

Siegfried Heintze

10/2/2002 5:49:00 AM

I don't understand the difference in thread safety between hashtable,
sortedlist and NamedValueCollection.

To quote the documentation "A Hashtable can safely support one writer and
multiple readers concurrently. To support multiple writers, all operations
must be done through the wrapper returned by the Synchronized method."

(1) Assuming I use the synchronized wrapper, does this mean I don't have to
use the lock statement in C# as long as there is a maximum of one writer?
What if I have multiple writers? Could I use a ReaderWriterLock?

How is this different than sorted list which says: "A SortedList can safely
support multiple readers concurrently, as long as the collection is not
modified. To guarantee the thread safety of the SortedList, all operations
must be done through the wrapper returned by the Synchronized method"

(2) How is this different from a Hashtable? Using the wrapper, reading does
not require a lock but writing does?

Now consider NamedValueCollection:
"This implementation does not provide a synchronized (thread-safe) wrapper
for a NameValueCollection, but derived classes can create their own
synchronized versions of the NameValueCollection using the
NameObjectCollectionBase.ICollection.SyncRoot property."

(3) Does this mean everything, must be done with a lock statement for
exclusive access? Why cannot two threads simultaneously read without
locking?


1 Answer

(Jian-Shen Lin[MS])

10/7/2002 7:13:00 AM

0

There are some different way to Synchronize the thread, most of them have
the same effect but may have different performance.

1. A Hashtable can safely support one writer and multiple readers
concurrently. To support multiple writers, all operations must be done
through this Hashtable.Synchronized Method wrapper only.

2. lock ensures that one thread does not enter a critical section while
another thread is in the critical section of code.

3. ReaderWriterLock supports the following features:

Use in large numbers, for activities such as per object synchronization.
Timeouts. This is a valuable feature to detect deadlocks.

Caching of events. This allows the events to be moved from the least
contentious regions to the most contentious regions. The number of events
needed by ReaderWriterLock is bounded by the number of threads in the
process.

Nested locks by readers and writers.



4. NameValueCollection does not provide a synchronized (thread-safe)
wrapper for a NameValueCollection, but derived classes can create their own
synchronized versions of the NameValueCollection using the
NameObjectCollectionBase.ICollection.SyncRoot property.

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.

There is KB talk about this issue
HOW TO: Implement Custom Collections in Visual C# .NET ID: Q307484

Best Regards

Jian Shen

This posting is provided "AS IS" with no warranties, and confers no rights.