473,785 Members | 2,641 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Threading Safety in System.Collecti ons

Could someone help explain thread safety issues in the System.Collecti ons
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 [howardsnewsATsp itzincDOTcom]
Software Engineer
Spitz, Inc [http://www.spitzinc.com]
Nov 17 '05 #1
2 2894
> 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 "recommende d" 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..jwaon line..com
-----------------------------------------------------------------------
"Howard Swope" <howardsnewsATs pitzincDOTcom> wrote in message news:ey******** ******@TK2MSFTN GP09.phx.gbl... Could someone help explain thread safety issues in the System.Collecti ons 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 [howardsnewsATsp itzincDOTcom]
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.Collecti ons 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*********@do tcomdatasolutio ns.com> wrote in message
news:u2******** ******@TK2MSFTN GP14.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 "recommende d" 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..jwaon line..com
-----------------------------------------------------------------------
"Howard Swope" <howardsnewsATs pitzincDOTcom> wrote in message
news:ey******** ******@TK2MSFTN GP09.phx.gbl...
Could someone help explain thread safety issues in the System.Collecti ons
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 [howardsnewsATsp itzincDOTcom]
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
5390
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 the moment. I'd be *very* grateful if people with any interest in multi-threading would read it (even just bits of it - it's somewhat long to go through the whole thing!) to check for accuracy, effectiveness of examples, etc. Feel free to mail...
1
2291
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 take a quick look at this and tell me where I'm going wrong? My actual use is more complex, but when I couldn't get that to work I created a new service project and figured to get a simple threading timer going first then I'd revamp my actual code...
8
13495
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 is the code below: i just wana display the simple array value stored in my array variable in my textbox thats it.
2
1329
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 written to by different threads in my example. It works fine so I'm wondering if anyone can tell me what I need to watch out for with respect to Forms and threading? For what it's worth, here is my short proggie: Private Sub SomeTask() ' This...
2
3980
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 the class fields by calling the update methods in the remoting object class and while thats not an issue I'm curious about threading with remote calls. I can't find much in the way of what threading issues can occur. If the service's thread...
13
1217
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. Suppose the worker thread displays a form and the thread that created it wants to change that form's text occasionally.
7
2377
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 finished. I am trying to follow this example : http://www.codeproject.com/cs/miscctrl/progressdialog.asp But although the messages still get moved, the progress window never does anything. Here is my code in full, if anybody who knows...
5
6936
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 sound. But my little app is a 'fake' app and is designed to look like another - hence this seeming crazy situation of needing to fake a progess bar. PROBLEM.
9
4122
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 in another class from the main class. So it couldn't access the Status Line or textbox. So what we did was set them up as properties: string IStatusDisplay.Status
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10325
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10148
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9950
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6740
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3646
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.