11

i have this code:

    Thread[] threadsArray = new Thread[4];
        for (int i = 0; i < 4; i++)
        {
            threadsArray[i] = new Thread(() => c1.k(i));
        }
        for (int i = 0; i < 4; i++)
        {
            threadsArray[i].Start();
        }
        for (int i = 0; i < 4; i++)
        {
            threadsArray[i].Join();
        }

the function k is this:

void k(int i)
{
    while(true)
      Console.WriteLine(i);
}

for some reason just the last thread is running and printing 4444444.... why aren't all the threads running?

4

3 Answers 3

30

All of the threads are printing the same variable.

Your lambda expression (() => c1.k(i)) captures the i variable by reference.
Therefore, when the lambda expression runs after i++, it picks up the new value of i.

To fix this, you need to declare a separate variable inside the loop so that each lambda gets its own variable, like this:

    for (int i = 0; i < 4; i++)
    {
        int localNum = i;
        threadsArray[i] = new Thread(() => c1.k(localNum));
    }
Sign up to request clarification or add additional context in comments.

2 Comments

+1 for this answer. It's basically the same as Winston's, but the explanation is more detailed.
On a side note: some languages like F# won't compile if you try to capture a mutable variable in a closure.
4

You are closing over the i variable.

Try this instead

for (int i = 0; i < 4; i++)
{
    int x = i;
    threadsArray[i] = new Thread(() => c1.k(x));
}

Comments

0
Thread[] threadsArray = new Thread[4];
for (int i = 0; i < 4; i++)
{
    //better use ParameterizedThreadStart Delegate
    threadsArray[i] = new Thread(k);
}
for (int i = 0; i < 4; i++)
{
    //passing the value to the function
    threadsArray[i].Start(i);
}
for (int i = 0; i < 4; i++)
{
    threadsArray[i].Join();
}

//changing the input data type
void k(object i)
{
    while (true)
        Console.WriteLine((int)i);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.