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

Threading Safety in System.Collections

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.

--
Howard Swope [howardsnewsATspitzincDOTcom]
Software Engineer
Spitz, Inc [http://www.spitzinc.com]
Nov 17 '05 #1
2 2871
> When the documentation says public static members of this type do they mean members of the collection class or static instances of
the class?
There is no such thing as static instances, so they mean "public static members". i.e. public static properties and fields. This
wording is used on most (if not all) of the class documentation in MSDN, so there may not actually be any public static members of
the interfaces or classes they are refering to.
Do public static members implement synchronization primitives to accomplish this or is there something inherintly thread safe
about the implementation of the static nature?
Static members are intrinsically thread-safe in .NET. No extra work is required for synchronized reads and writes of those members.
What is the Synchronized() method doing to create the wrapper?
The "recommended" approach for synchronizing collections as used by those framework classes that apply is to have a private class
(nested) that derives from the collection that may be synchronized and overrides those members that require synchronization. This
is usually done by locking "this" (or Synlock Me in VB) for the complete base.[Member] call of the member being synchronized.
What is the nature of the SyncRoot object?
It provides the object that the collection is using internally to synchronize those wrapped methods. As I mentioned above, in all
cases I'm aware of, this will be the instance of the collection itself.
In most cases I am inheriting from a collection class. Are there issues specific to inheritence that I should be aware of?
You may inherit from collection classes. This is why they aren't "sealed". In some cases it makes more sense to implement one of
the interfaces as opposed to deriving from one of the predefined implementations.

I don't think there are any "issues specific to inheritance" that apply only to collections.

--
Dave Sexton
dave@www..jwaonline..com
-----------------------------------------------------------------------
"Howard Swope" <howardsnewsATspitzincDOTcom> wrote in message news:ey**************@TK2MSFTNGP09.phx.gbl... 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.

--
Howard Swope [howardsnewsATspitzincDOTcom]
Software Engineer
Spitz, Inc [http://www.spitzinc.com]

Nov 17 '05 #2
Dave:

I tried to send you email to thank you. I removed the NOSPAM- from you email
address, but it didn't go through for some reason. Anyway... thanks for the
information. It was very helpful. Life was much easier when I encapsulated
the System.Collections class and implemented ICollection and IEnumerable. I
grabbed the SyncRoot object and made the whole thing nice and thread safe.
Life is good.

"Dave" <NO*********@dotcomdatasolutions.com> wrote in message
news:u2**************@TK2MSFTNGP14.phx.gbl...
When the documentation says public static members of this type do they
mean members of the collection class or static instances of the class?


There is no such thing as static instances, so they mean "public static
members". i.e. public static properties and fields. This wording is used
on most (if not all) of the class documentation in MSDN, so there may not
actually be any public static members of the interfaces or classes they
are refering to.
Do public static members implement synchronization primitives to
accomplish this or is there something inherintly thread safe about the
implementation of the static nature?


Static members are intrinsically thread-safe in .NET. No extra work is
required for synchronized reads and writes of those members.
What is the Synchronized() method doing to create the wrapper?


The "recommended" approach for synchronizing collections as used by those
framework classes that apply is to have a private class (nested) that
derives from the collection that may be synchronized and overrides those
members that require synchronization. This is usually done by locking
"this" (or Synlock Me in VB) for the complete base.[Member] call of the
member being synchronized.
What is the nature of the SyncRoot object?


It provides the object that the collection is using internally to
synchronize those wrapped methods. As I mentioned above, in all cases I'm
aware of, this will be the instance of the collection itself.
In most cases I am inheriting from a collection class. Are there issues
specific to inheritence that I should be aware of?


You may inherit from collection classes. This is why they aren't
"sealed". In some cases it makes more sense to implement one of the
interfaces as opposed to deriving from one of the predefined
implementations.

I don't think there are any "issues specific to inheritance" that apply
only to collections.

--
Dave Sexton
dave@www..jwaonline..com
-----------------------------------------------------------------------
"Howard Swope" <howardsnewsATspitzincDOTcom> wrote in message
news:ey**************@TK2MSFTNGP09.phx.gbl...
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.

--
Howard Swope [howardsnewsATspitzincDOTcom]
Software Engineer
Spitz, Inc [http://www.spitzinc.com]


Nov 17 '05 #3

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

Similar topics

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...
1
by: Tom | last post by:
I've googled, and read, and stripped out code, and rewritten code, and still can't get a System.Threading.Timer to work. (I hereby publicly admit that I'm a failure here...) Could someone please...
8
by: Yatharth | last post by:
Hi, I m new to threading and i have successfully runed threading but i could display value on my web page ,but its working in code behind when i see it through debugger,plzzzzzzz help me here...
2
by: Tyson Ackland | last post by:
I have written a very simple threading proggie in an attempt to teach myself threading. I have seen it referred to in articles that Forms are not thread safe. My form has two labels which are...
2
by: greg.merideth | last post by:
I have a project where we have a windows service that creates a remoting object for an external client application to communicate with using ipc. We've discovered the client is making updates to...
13
by: **Developer** | last post by:
I have a simple need that I can't seem to locate the answer to in the docs. Most examples show how a worker thread can pass data back to the thread that created it. I need to do the opposite....
7
by: Mike P | last post by:
I am trying to write my first program using threading..basically I am moving messages from an Outlook inbox and want to show the user where the process is up to without having to wait until it has...
5
by: CCLeasing | last post by:
For an application I'm creating I want to create a 'fake' progress bar. By fake I mean a progress bar that looks like it's doing something but actually isn't. I know philosophically this isn't...
9
by: tshad | last post by:
I have a Windows App that is doing some work and then writing a "Now Processing..." line to the status line of the window as well as the Textbox on the form. But the problem is that the work is...
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...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.