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 6039807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:25:24+00:00 2026-05-23T06:25:24+00:00

Updated question to be more generic: I have the following code. When you swap

  • 0

Updated question to be more generic:

I have the following code. When you swap the location of threads[i].Join() you get different output.

static void ThreadedWorker(int startIndex, int endIndex)
{
    Console.WriteLine("Working from results[ " + startIndex +"] to results["+endIndex+"]");
}

static void Main(string[] args)
{
    int threadCount = System.Environment.ProcessorCount;
    int calculationCount = 500; //the number of array elements we'd be iterating over if we were doing our work
    int threadDataChunkSize = calculationCount / threadCount;
    if (threadDataChunkSize < 1) threadDataChunkSize = 1; //just in case we have loads of threads

    Thread[] threads = new Thread[threadCount];
    for (int i = 0; i < threadCount; i++)
    {
        threads[i] = new Thread(() => ThreadedWorker(threadDataChunkSize * i, threadDataChunkSize*(i+1)));
        threads[i].Start();
        //threads[i].Join(); //****Uncomment for correct behaviour****
    }

    for (int i = 0; i < threadCount; i++)
    {
        //threads[i].Join(); //****Uncomment for incorrect behaviour****
    }

    Console.WriteLine("breakhere");
}

When Join() is in the first loop, creating sequential behaviour, you get the output

Working from results[ 0] to results[125]
Working from results[ 125] to results[250]
Working from results[ 250] to results[375]
Working from results[ 375] to results[500]

When Join() is in the second loop, creating parallel behaviour, you get non deterministic output something like:

Working from results[ 375] to results[500]
Working from results[ 375] to results[500]
Working from results[ 500] to results[625]
Working from results[ 500] to results[625] (i is sometimes more than it should ever be!)

My suspicion is that lambda expression somehow causes the problem. Hopefully this rephrasing also demonstrates that this is not a bounds miscalculation, or other abuse of my arrays!


Initial question was less generic and used the startIndex and endIndex to iterate over a byte array doing work. I described the ThreadedWorker as ‘not working’, as it seemed to sometimes update the result array and sometimes not. It now appears that it was called but the startindex and endindex were mangled.

  • 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-23T06:25:25+00:00Added an answer on May 23, 2026 at 6:25 am

    Your solution is correct, but you’re misunderstanding the problem.

    Lambda expressions are thread-safe.

    However, all of your lambda expressions are sharing the same i variable.
    Therefore, if one of the threads happens to start after the loop moves on to the next iteration, it will pick up the newer value of i.

    By declaring a separate variable inside the loop, you’re forcing each lambda to use its own variable, which never changes.

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

Sidebar

Related Questions

Question Updated I have a generic list which can contains the following values: Sector
I don't have a specific question here but I'm more looking for some guidance
Update : Gutted entire question with more thorough description Ok same question with different
I have some urls like http://www.domainname.com/m1.php?#m2.php?more=apple&starting=1 How to use PHP to get the value
Here is the updated question: the current query is doing something like: $sql1 =
Replaces Question: Update multiple rows into SQL table Here's a Code Snippet to update
I have a question regarding an update function I created... CREATE OR REPLACE FUNCTION
I have the following problem that other people must have encountered. I am working
Example, I have the following interface and classes: public interface IRole { DateTime Since
i have a question regarding some complex data-binding. I want to be able to

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.