Thread-safe enumeration thru collection?

G

Grandma Wilkerson

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?
 
R

Ryan Byington

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

--------------------
 
M

mia lanui

just lock() the critical sections on SyncRoot and you don't need to worry

Ryan Byington said:
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" <[email protected]>
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: <[email protected]>
Newsgroups: microsoft.public.dotnet.general,microsoft.public.dotnet.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!tk2msftngp13.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 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?
 
C

Chad Myers

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 said:
just lock() the critical sections on SyncRoot and you don't need to worry

Ryan Byington said:
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
microsoft.public.dotnet.general,microsoft.public.dotnet.languages.csharp microsoft.public.dotnet.languages.csharp:169990
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top