Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6872121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:53:17+00:00 2026-05-27T03:53:17+00:00

This is just something that’s been puzzling me ever since I read about iterators

  • 0

This is just something that’s been puzzling me ever since I read about iterators on Jon Skeet’s site.

There’s a simple performance optimisation that Microsoft has implemented with their automatic iterators – the returned IEnumerable can be reused as an IEnumerator, saving an object creation. Now because an IEnumerator necessarily needs to track state, this is only valid the first time it’s iterated.

What I cannot understand is why the design team took the approach they did to ensure thread safety.

Normally when I’m in a similar position I’d use what I consider to be a simple Interlocked.CompareExchange – to ensure that only one thread manages to change the state from “available” to “in process”.

Conceptually it’s very simple, a single atomic operation, no extra fields are required etc.

But the design teams approach? Every IEnumerable keeps a field of the managed thread ID of the creating thread, and then that thread ID is checked on calling GetEnumerator against this field, and only if it’s the same thread, and it’s the first time it’s called, can the IEnumerable return itself as the IEnumerator. It seems harder to reason about, imo.

I’m just wondering why this approach was taken. Are Interlocked operations far slower than two calls to System.Threading.Thread.CurrentThread.ManagedThreadId, so much so that it justifies the extra field?

Or is there some other reason behind this, perhaps involving memory models or ARM devices or something I’m not seeing? Maybe the spec imparts specific requirements on the implementation of IEnumerable? Just genuinely puzzled.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T03:53:17+00:00Added an answer on May 27, 2026 at 3:53 am

    I can’t answer definatively, but as to your question:

    Are Interlocked operations far slower than two calls to
    System.Threading.Thread.CurrentThread.ManagedThreadId, so much so that
    it justifies the extra field?

    Yes interlocked operations are much slower that two calls to get the ManagedThreadId – interlocked operations aren’t cheap because they required multi-CPU systems to synchonize their caches.

    From Understanding the Impact of Low-Lock Techniques in Multithreaded Apps:

    Interlocked instructions need to ensure that caches are synchronized
    so that reads and writes don’t seem to move past the instruction.
    Depending on the details of the memory system and how much memory was
    recently modified on various processors, this can be pretty expensive
    (hundreds of instruction cycles).

    In Threading in C#, it lists overhead the overhead as 10ns. Whereas getting the ManagedThreadId should be a normal non-locked read of static data.

    Now this is just my speculation, but if you think about the normal use case it would be to call the function to retrieve the IEnumerable and immediately iterative over it once. So in the standard use case the object is:

    1. Used once
    2. Used on the same thread it was created
    3. Short lived

    So this design brings in no synchronization overhead and sacrifices 4 bytes, which will probably only be in use for a very short period of time.

    Of course to prove this you would have to do performance analysis to determine the relative costs and code analysis to prove what the common case was.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question is just something that I have been thinking about lately. Can a
This is probably a pretty basic question, but just something that I wanted to
I have just found this great tutorial as it is something that I need.
This is just a simple question. I've been reading the source of something which
I've been beating my head against this, and I'm sure I'm just missing something
Just something that i was wondering about. In Europe the comma us mostly used
This just seems like something that is easy but I can't find any data
This is not so much a problem, just something that I would like to
This is probably a pretty basic question, but just something that I wanted to
This is just something that has bothered me for the last couple of days,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.