Daniel O'Connell [C# MVP]
10/13/2003 9:30:00 PM
"Lloyd Dupont" <net.galador@ld> wrote in message
news:OIy1u6XkDHA.2964@tk2msftngp13.phx.gbl...
> I like this int version inside a list, but if my list is gong to be used
in
> multithreaded environment, ain't all these lock() {} going to be a
> performance hit ?
>
Yes. I however advised using the proper locking because I didn't know what
you were doing precisely. If your class requires locking in other
situations(it needs to be thread safe, for example) then use locks, if not,
Interlocked.Increment(ref int) is quite sufficent for simply incrementing
the version field.
> "Daniel O'Connell" <onyxkirx@--NOSPAM--comcast.net> a $BqD(Brit dans le
message
> de news:fTrib.550641$Oz4.497665@rwcrnsc54...
> >
> > "Lloyd Dupont" <net.galador@ld> wrote in message
> > news:ea8V0QUkDHA.2268@TK2MSFTNGP12.phx.gbl...
> > > Does any of you have the slightest ideas of why you can't modify an
> array
> > > list while in a foreach of its element.
> > >
> > Because that is the rules when it comes to enumerators, it makes
> enumerator
> > implementation alot easier. Allowing changes requires a more
> sophisticated,
> > thread-safe enumerator. The probability of a bug in that case would be
> very
> > large in deed.
> > > I wrote my own collection and I'm trying to add the same behavior but
I
> > > don't know where to start.
> > >
> > > I though to internal events but dismiss the idea because it prevents
> > garbage
> > > collection of the enumerator.
> > > any other ideas ?
> > >
> > Well, simple method, stick a private field like int version; in your
class
> > and increment it on every change, following your proper locking rules
and
> > methodologies of course(you may need to use
> > System.Threading.Interlocked.Increment in this case, assuming multiple
> > threads, enumerators, and processors). Then on every command to the
> > enumerator check that value, if it changed since the enumerator was
> created,
> > your enumerator is invalid and should throw an exception as specified in
> the
> > IEnumerable documentation.
> >
> >
> > >
> >
> >
>
>