Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

That is because your different threads are not using the same `Enumerator`. I'm guessing your threads look something like this, based on your mention of `foreach`:

    // In multiple threads:
    foreach(element in collection) { ... }
`collection` is not an `Enumerator` but rather an `Enumerable`. The `foreach` statement calls `collection.GetEnumerator`, so each thread gets its own iterator. You would only see inconsistencies if another thread modified the collection during iteration.

Try this instead, and you'll guaranteed see issues:

    // Before:
    IEnumerator enumerator = collection.getEnumerator();

    // In multiple threads:
    while(enumerator.Move()) {
        var element = enumerator.Current;
    }


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: