By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,887 Members | 954 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,887 IT Pros & Developers. It's quick & easy.

Thread-safe enumeration thru collection?

P: n/a
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?

Nov 13 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
The following is taken from the SDK:
"An enumerator remains valid as long as the collection remains unchanged."

I don't see why any there would be any problem having multiple enumerators
in different threads on the same collection while nothing is modifying the
collection.

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.csharpNNTP-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:101593X-Tomcat-NG: microsoft.public.dotnet.languages.csharp

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?



Nov 15 '05 #2

P: n/a
just lock() the critical sections on SyncRoot and you don't need to worry

"Ryan Byington" <ry*****@online.microsoft.com> wrote in message
news:tk**************@cpmsftngxa06.phx.gbl...
The following is taken from the SDK:
"An enumerator remains valid as long as the collection remains unchanged."

I don't see why any there would be any problem having multiple enumerators
in different threads on the same collection while nothing is modifying the
collection.

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.languages.csharp

Hi,

The documentation states that enumeration through a collection is
inherently NOT thread-safe, since a thread which added/removed an item

fromsaid 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 writingto) the collection using foreach()?

I have a class that exposes an event [which is really just a collectionof 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?


Nov 15 '05 #3

P: n/a
Having every reader lock on the collection is inefficient.

What you might consider is a ReaderWriterLock. This allows
multiple readers and a single writer.

When a writer wants to write, it blocks until all the
readers are done (new readers are blocked) and then the
writer gets its turn. When the writer is done, then the
readers may resume.

-c

"mia lanui" <mi**@email.com> wrote in message
news:op*****************@nwrdny02.gnilink.net...
just lock() the critical sections on SyncRoot and you don't need to worry
"Ryan Byington" <ry*****@online.microsoft.com> wrote in message
news:tk**************@cpmsftngxa06.phx.gbl...
The following is taken from the SDK:
"An enumerator remains valid as long as the collection remains unchanged."

I don't see why any there would be any problem having multiple enumerators in different threads on the same collection while nothing is modifying the collection.

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.gblXref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:169990
microsoft.public.dotnet.general:101593
X-Tomcat-NG: microsoft.public.dotnet.languages.csharp

Hi,

The documentation states that enumeration through a collection isinherently NOT thread-safe, since a thread which added/removed an item fromsaid collection could screw up the thread that was iterating. That
makessense.. but... given a collection that is filled at start-time and nevermodified again, is it safe to have multiple threads *reading* (not

writingto) the collection using foreach()?

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



Nov 15 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.