Thread-safe enumeration thru collection?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Grandma Wilkerson

    #1

    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?





  • Ryan Byington

    #2
    RE: Thread-safe enumeration thru collection?

    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

    --------------------[color=blue]
    >From: "Grandma Wilkerson" <turd@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: <usAXCJKTDHA.37 96@tk2msftngp13 .phx.gbl>
    >Newsgroups:[/color]
    microsoft.publi c.dotnet.genera l,microsoft.pub lic.dotnet.lang uages.csharp[color=blue]
    >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[/color]
    microsoft.publi c.dotnet.genera l:101593[color=blue]
    >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 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?
    >
    >
    >
    >
    >
    >[/color]

    Comment

    • mia lanui

      #3
      Re: Thread-safe enumeration thru collection?

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

      "Ryan Byington" <ryanbyi@online .microsoft.com> wrote in message
      news:tkFp3grYDH A.2432@cpmsftng xa06.phx.gbl...[color=blue]
      > 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[/color]
      at[color=blue]
      > http://www.microsoft.com/info/cpyright.htm
      >
      > --------------------[color=green]
      > >From: "Grandma Wilkerson" <turd@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: <usAXCJKTDHA.37 96@tk2msftngp13 .phx.gbl>
      > >Newsgroups:[/color]
      > microsoft.publi c.dotnet.genera l,microsoft.pub lic.dotnet.lang uages.csharp[color=green]
      > >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[/color][/color]
      microsoft.publi c.dotnet.langua ges.csharp:1699 90[color=blue]
      > microsoft.publi c.dotnet.genera l:101593[color=green]
      > >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[/color][/color]
      from[color=blue][color=green]
      > >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[/color][/color]
      writing[color=blue][color=green]
      > >to) the collection using foreach()?
      > >
      > > I have a class that exposes an event [which is really just a[/color][/color]
      collection[color=blue][color=green]
      > >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?
      > >
      > >
      > >
      > >
      > >
      > >[/color]
      >[/color]


      Comment

      • Chad Myers

        #4
        Re: Thread-safe enumeration thru collection?

        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" <mial@email.com > wrote in message
        news:opW_a.9304 $u%2.9099@nwrdn y02.gnilink.net ...[color=blue]
        > just lock() the critical sections on SyncRoot and you don't need to[/color]
        worry[color=blue]
        >
        > "Ryan Byington" <ryanbyi@online .microsoft.com> wrote in message
        > news:tkFp3grYDH A.2432@cpmsftng xa06.phx.gbl...[color=green]
        > > The following is taken from the SDK:
        > > "An enumerator remains valid as long as the collection remains[/color][/color]
        unchanged."[color=blue][color=green]
        > >
        > > I don't see why any there would be any problem having multiple[/color][/color]
        enumerators[color=blue][color=green]
        > > in different threads on the same collection while nothing is[/color][/color]
        modifying the[color=blue][color=green]
        > > 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[/color][/color]
        specified[color=blue]
        > at[color=green]
        > > http://www.microsoft.com/info/cpyright.htm
        > >
        > > --------------------[color=darkred]
        > > >From: "Grandma Wilkerson" <turd@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: <usAXCJKTDHA.37 96@tk2msftngp13 .phx.gbl>
        > > >Newsgroups:[/color]
        > >[/color][/color]
        microsoft.publi c.dotnet.genera l,microsoft.pub lic.dotnet.lang uages.csharp[color=blue][color=green][color=darkred]
        > > >NNTP-Posting-Host: rrcs-west-66-27-51-213.biz.rr.com 66.27.51.213
        > > >Path:[/color][/color][/color]
        cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!tk2 msftngp13.phx.g bl[color=blue][color=green][color=darkred]
        > > >Xref: cpmsftngxa06.ph x.gbl[/color][/color]
        > microsoft.publi c.dotnet.langua ges.csharp:1699 90[color=green]
        > > microsoft.publi c.dotnet.genera l:101593[color=darkred]
        > > >X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
        > > >
        > > >Hi,
        > > >
        > > > The documentation states that enumeration through a collection[/color][/color][/color]
        is[color=blue][color=green][color=darkred]
        > > >inherently NOT thread-safe, since a thread which added/removed an[/color][/color][/color]
        item[color=blue]
        > from[color=green][color=darkred]
        > > >said collection could screw up the thread that was iterating. That[/color][/color][/color]
        makes[color=blue][color=green][color=darkred]
        > > >sense.. but... given a collection that is filled at start-time and[/color][/color][/color]
        never[color=blue][color=green][color=darkred]
        > > >modified again, is it safe to have multiple threads *reading* (not[/color][/color]
        > writing[color=green][color=darkred]
        > > >to) the collection using foreach()?
        > > >
        > > > I have a class that exposes an event [which is really just a[/color][/color]
        > collection[color=green][color=darkred]
        > > >of delegates]. The event registrants are all hooked up at startup.[/color][/color][/color]
        Is it[color=blue][color=green][color=darkred]
        > > >safe for my class to fire the event simultaneously from two[/color][/color][/color]
        different[color=blue][color=green][color=darkred]
        > > >threads even though this would lead to the delegate collection[/color][/color][/color]
        being[color=blue][color=green][color=darkred]
        > > >iterated by two different threads?
        > > >
        > > >
        > > >
        > > >
        > > >
        > > >[/color]
        > >[/color]
        >
        >[/color]


        Comment

        Working...