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

  • SEARCH
  • Home
  • 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 8371513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:10:44+00:00 2026-06-09T14:10:44+00:00

In my code below, the Id property of ThreadClass is not set deterministically as

  • 0

In my code below, the Id property of ThreadClass is not set deterministically as expected (ThreadArray[0]'s ThreadClass.Id = 0, ThreadArray[1]'s ThreadClass.Id = 1, etc).

If I debug and slow down the Thread.Start()'s, everything works as expected. But when the program runs at full-speed, I get all Id's = 4 (or similar). I can’t lock i because it’s not a reference variable. Clearly, I am encountering a race condition. What am I doing wrong?

Main.cs

for (int i = 0; i < ThreadCount; i++)
{
    ThreadArray[i] = new Thread(() =>
        {
            new ThreadClass(i);
        });
    ThreadArray[i].Start();
}

ThreadClass.cs

private int Id { get; set; }

public ThreadClass(int i) {
    Id = id;
    while(true)
    {
        Console.WriteLine("I am thread " + i");
        Thread.Sleep(5000);
    }
}

Expected output:

I am thread 0
I am thread 1
I am thread 2
I am thread 3
... 5 second wait ...
I am thread 0
I am thread 1
I am thread 2
I am thread 3

Actual output:

I am thread 4
I am thread 4
I am thread 4
I am thread 4
... 5 second wait ...
I am thread 4
I am thread 4
I am thread 4
I am thread 4

Note that at this point each instance of ThreadArray is initialized to a valid Thread object.

  • 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-06-09T14:10:46+00:00Added an answer on June 9, 2026 at 2:10 pm

    You’re using a version of C# that doesn’t close over a fresh copy of the i variable. By the time the threads are executed, i is at or near ThreadCount. Simply create a copy of the variable so the closure can capture that instead:

    for (int i = 0; i < ThreadCount; i++) 
    {  
        int temp = i;
        ThreadArray[i] = new Thread(() =>  
            {              
                new ThreadClass(temp); 
            });      
    }  
    

    By the way, the version of C# in VS 2012 (or .NET 4.5) fixes this. See http://msdn.microsoft.com/en-us/library/hh678682(v=vs.110).aspx

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

Sidebar

Related Questions

The code below gives an error: Property 'Int32 Key' is not defined for type
I've some simple code below that uses a ToggleButton.IsChecked property to set the Visibility
Is it possible to shorthand the code below? Essentially metaItem.Text is a string property
I'm wondering about the .childNodes property, I have the code below, and for some
In C# 3.0, I'm doing the code below to declare a DateTime property and
In the below code, MousePressImage is a dependency property of class ButtonControl . The
I got below code from http://msdn.microsoft.com/en-us/library/dd584174(office.11).aspx for adding custom property in webpart tool pane.
I am trying to map the ReferralContract.AssessmentId property to Referral.Assessment.Id The below code works
In the code below I get the correct type of the property ( PropertyA
i have trouble with optimization of the code below which is changing css property

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.