467,075 Members | 1,019 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,075 developers. It's quick & easy.

Thread-safe enumeration thru collection?

Hi,

The documentation states that enumeration through a collection is
inherently NOT thread-safe, since a thread which added/removed an item from
said collection could screw up the thread that was iterating. That makes
sense.. but... given a collection that is filled at start-time and never
modified again, is it safe to have multiple threads *reading* (not writing
to) the collection using foreach()?

I have a class that exposes an event [which is really just a collection
of delegates]. The event registrants are all hooked up at startup. Is it
safe for my class to fire the event simultaneously from two different
threads even though this would lead to the delegate collection being
iterated by two different threads?

Jul 19 '05 #1
  • viewed: 1847
Share:
1 Reply
What you described should work. There should be no reason why a collection
would have a problem with multiple threads iterating through it.

Ryan Byington [MS]

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
--------------------
| From: "Grandma Wilkerson" <tu**@bottleneck.scalability.crash.dum>
| Subject: Thread-safe enumeration thru collection?
| Date: Thu, 17 Jul 2003 13:30:25 -0700
| Lines: 19
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <us**************@tk2msftngp13.phx.gbl>
| Newsgroups:
microsoft.public.dotnet.general,microsoft.public.d otnet.languages.csharp
| NNTP-Posting-Host: rrcs-west-66-27-51-213.biz.rr.com 66.27.51.213
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftn gp13.phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.languages.csharp:169990
microsoft.public.dotnet.general:101593
| X-Tomcat-NG: microsoft.public.dotnet.general
|
| Hi,
|
| The documentation states that enumeration through a collection is
| inherently NOT thread-safe, since a thread which added/removed an item
from
| said collection could screw up the thread that was iterating. That makes
| sense.. but... given a collection that is filled at start-time and never
| modified again, is it safe to have multiple threads *reading* (not writing
| to) the collection using foreach()?
|
| I have a class that exposes an event [which is really just a
collection
| of delegates]. The event registrants are all hooked up at startup. Is it
| safe for my class to fire the event simultaneously from two different
| threads even though this would lead to the delegate collection being
| iterated by two different threads?
|
|
|
|
|
|

Jul 21 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by adeger | last post: by
4 posts views Thread by Gilles Leblanc | last post: by
7 posts views Thread by Ivan | last post: by
4 posts views Thread by Matthew Groch | last post: by
16 posts views Thread by droopytoon@hotmail.com | last post: by
9 posts views Thread by mareal | last post: by
13 posts views Thread by Bob Day | last post: by
7 posts views Thread by Charles Law | last post: by
4 posts views Thread by jayesah@gmail.com | last post: by
3 posts views Thread by John Nagle | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.