What is the best way to make extended IEnumerator safe?

  • Thread starter Thread starter Sasha
  • Start date Start date
S

Sasha

Hi,

I am extending standard IEnumerator, and I was just wondering what is the
best way to make enumarator safe? What do I mean by safe? Detect deletes and
all...
My idea is to have private Guid state field in the collection, and every
time something is inserted or deleted from the collection, I will just
change the guid. Enumerator will just have to compare the guid received in
the begging to the current one. If they are different, the collection has
changed.

What are your thought on this?

Thank you,
Sasha
 
Sasha,

That sounds like a good idea, and it would be quick and easy as well.
The only things you have to keep in mind is to lock the collections or
rather make sure that the guid is accessed correctly by multiple threads.

Hope this helps.
 
Sasha said:
Hi,

I am extending standard IEnumerator, and I was just wondering what is
the best way to make enumarator safe? What do I mean by safe? Detect
deletes and all...

Why?
 
Otherwise enumerator will have a logical error.

What if someone added some stuff, but we already passed that area. We will
miss one item...

Take Care,
Sasha
 
Hi Sasha,

I think your idea is a good thought.

But now, for thread safety, I think you should lock the entire collection
while
enumerating.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: "Sasha" <[email protected]>
| References: <[email protected]>
<[email protected]>
| Subject: Re: What is the best way to make extended IEnumerator safe?
| Date: Thu, 25 Sep 2003 17:31:46 -0800
| Lines: 22
| 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.languages.csharp
| NNTP-Posting-Host: filenet-gw.filenet.com 198.3.8.1
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:187445
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Otherwise enumerator will have a logical error.
|
| What if someone added some stuff, but we already passed that area. We will
| miss one item...
|
| Take Care,
| Sasha
|
| | > Sasha wrote:
| > > Hi,
| > >
| > > I am extending standard IEnumerator, and I was just wondering what is
| > > the best way to make enumarator safe? What do I mean by safe? Detect
| > > deletes and all...
| >
| > Why?
| >
| >
|
|
|
 
Hi Sasha,

You've posted this same question separately in the languages.vb group. You
are not allowing the VB people to get the benefit of the CSharp people's
thinking, and vice versa. It fragments the discussion. It also means that
people like myself, who frequent both camps, have to decide which to post
comments into or to have to post to both.

It's not helped by the fact that this is occuring when the newsgroup
server and Outkooky Express are collaborating in keeping everyone confused as
to what exists and what has been deleted!!

Please, for future discussions (and they are most welcome), post to all
groups at the saame time.

Thanks. :-)

Regards,
Fergus
 
Sasha said:
Otherwise enumerator will have a logical error.

What if someone added some stuff, but we already passed that area. We
will miss one item...

That is clearly spelled out in the documentation on IEnumerator: changes
to the underlying collection invalidate the current enumerator and
causes an exception to be thrown.

What you're calling "safe" is completely counter to the definition an
enumerator.
 
Thus spake Jeffrey Tan[MSFT]:
But now, for thread safety, I think you should lock the entire
collection while enumerating.

If the collection is locked, only the current thread would be capable of
modifying it. So if it's your own code that's modifying the collection
during iteration, how are you going to miss it?
 
The simplest thing to do here is to keep an operation count as a member of
the collection, and increment it every time you modify the collection. The
enumerator stores the number when it starts an interation, and throws an
exception if the value changes.

--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://blogs.gotdotnet.com/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Frank,

If there are multi-threading in your code to access it, you can design it
well,
making them visit it synchronization.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: "Frank Oquendo" <[email protected]>
| References: <[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
| Subject: Re: What is the best way to make extended IEnumerator safe?
| Date: Fri, 26 Sep 2003 09:03:30 -0500
| Lines: 16
| 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.languages.csharp
| NNTP-Posting-Host: unassigned-207-178-108-33.hubris.net 207.178.108.33
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:187546
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Thus spake Jeffrey Tan[MSFT]:
|
| > But now, for thread safety, I think you should lock the entire
| > collection while enumerating.
|
| If the collection is locked, only the current thread would be capable of
| modifying it. So if it's your own code that's modifying the collection
| during iteration, how are you going to miss it?
|
| --
| There are 10 kinds of people. Those who understand binary and those who
| don't.
|
| http://code.acadx.com
|
|
|
 
Back
Top