473,837 Members | 1,549 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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?

Nov 13 '05 #1
3 7984
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**@bottlenec k.scalability.c rash.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.publi c.dotnet.genera l,microsoft.pub lic.dotnet.lang uages.csharpNNTP-Posting-Host: rrcs-west-66-27-51-213.biz.rr.com 66.27.51.213
Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!tk2 msftngp13.phx.g bl
Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.csharp:1699 90 microsoft.publi c.dotnet.genera l:101593X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.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
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******** ******@cpmsftng xa06.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**@bottlenec k.scalability.c rash.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.publi c.dotnet.genera l,microsoft.pub lic.dotnet.lang uages.csharp
NNTP-Posting-Host: rrcs-west-66-27-51-213.biz.rr.com 66.27.51.213
Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!tk2 msftngp13.phx.g bl
Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.csharp:1699 90 microsoft.publi c.dotnet.genera l:101593
X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.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
Having every reader lock on the collection is inefficient.

What you might consider is a ReaderWriterLoc k. 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******** *********@nwrdn y02.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******** ******@cpmsftng xa06.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**@bottlenec k.scalability.c rash.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.publi c.dotnet.genera l,microsoft.pub lic.dotnet.lang uages.csharp
NNTP-Posting-Host: rrcs-west-66-27-51-213.biz.rr.com 66.27.51.213
Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!tk2 msftngp13.phx.g blXref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.csharp:1699 90
microsoft.publi c.dotnet.genera l:101593
X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
2186
by: adeger | last post by:
Having trouble with my first forays into threads. Basically, the threads don't seem to be working in parallel (or you might say are blocking). I've boiled my problems to the following short code block and ensuing output. Seems like the output should be all interleaved and of course it's not. Running Python 2.2 from ActiveState on Windows XP (also doesn't work on Windows 2000). Thanks in advance! adeger
4
2899
by: Gilles Leblanc | last post by:
Hi I have started a small project with PyOpenGL. I am wondering what are the options for a GUI. So far I checked PyUI but it has some problems with 3d rendering outside the Windows platform. I know of WxPython but I don't know if I can create a WxPython window, use gl rendering code in it and then put widgets on top of that...
7
2710
by: Ivan | last post by:
Hi I have following problem: I'm creating two threads who are performing some tasks. When one thread finished I would like to restart her again (e.g. new job). Following example demonstrates that. Problem is that when program is started many threads are created (see output section), when only two should be running at any time. Can you please help me to identify me where the problem is? Best regards
4
5441
by: Matthew Groch | last post by:
Hi all, I've got a server that handles a relatively high number of concurrent transactions (on the magnitude of 1000's per second). Client applications establish socket connections with the server. Data is sent and received over these connections using the asynchronous model. The server is currently in beta testing. Sporadically over the course of the day, I'll observe the thread count on the process (via perfmon) start climbing....
16
3318
by: droopytoon | last post by:
Hi, I start a new thread (previous one was "thread timing") because I have isolated my problem. It has nothing to do with calling unmanaged C++ code (I removed it in a test application). I have a thread "_itTaskThread" running. The application is listening on a TCP port. It accepts 2 connection from a client. I simulate a crash on the client side (using "Debug->StopDebugging").
9
3080
by: mareal | last post by:
I have noticed how the thread I created just stops running. I have added several exceptions to the thread System.Threading.SynchronizationLockException System.Threading.ThreadAbortException System.Threading.ThreadInterruptedException System.Threading.ThreadStateException to see if I could get more information about why the thread stops running but that code is never executed. Any ideas on how I can debug this?
13
5103
by: Bob Day | last post by:
Using vs2003, vb.net I start a thread, giving it a name before start. Code snippet: 'give each thread a unique name (for later identification) Trunk_Thread.Name = "Trunk_0_Thread" ' allow only 1 thread per line Trunk_Thread.ApartmentState = ApartmentState.STA
7
2701
by: Charles Law | last post by:
My first thought was to call WorkerThread.Suspend but the help cautions against this (for good reason) because the caller has no control over where the thread actually stops, and it might have a lock pending, for example. I want to be able to stop a thread temporarily, and then optionally resume it or stop it for good.
4
6226
by: jayesah | last post by:
Hi All, I am writting a Thread class with using pthread library. I have some problem in saving thread function type and argument type. How to make Thread class generic ? /* This is my global Function */ template < class FunType, class ArgType> Thread makeThread(Funtype fun, ArgType arg)
3
35528
by: John Nagle | last post by:
There's no way to set thread priorities within Python, is there? We have some threads that go compute-bound, and would like to reduce their priority slightly so the other operations, like accessing the database and servicing queries, aren't slowed as much. John Nagle
0
9837
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
9682
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10870
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
9392
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7804
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6998
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
5668
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...
0
5845
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4474
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

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.