H
Howard Swope
Could someone help explain thread safety issues in the System.Collections
classes? The documentation states:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Thread Safety
Public static (Shared in Visual Basic) members of this type are safe for
multithreaded operations. Instance members are not guaranteed to be
thread-safe.
A SortedList can 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.
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.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
When the documentation says public static members of this type do they mean
members of the collection class or static instances of the class?
Do public static members implement synchronization primitives to accomplish
this or is there something inherintly thread safe about the implementation
of the static nature?
What is the Synchronized() method doing to create the wrapper?
What is the nature of the SyncRoot object?
In most cases I am inheriting from a collection class. Are there issues
specific to inheritence that I should be aware of?
Thanks.
classes? The documentation states:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Thread Safety
Public static (Shared in Visual Basic) members of this type are safe for
multithreaded operations. Instance members are not guaranteed to be
thread-safe.
A SortedList can 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.
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.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
When the documentation says public static members of this type do they mean
members of the collection class or static instances of the class?
Do public static members implement synchronization primitives to accomplish
this or is there something inherintly thread safe about the implementation
of the static nature?
What is the Synchronized() method doing to create the wrapper?
What is the nature of the SyncRoot object?
In most cases I am inheriting from a collection class. Are there issues
specific to inheritence that I should be aware of?
Thanks.