Threads receiving wrong parameters
Asked Answered
D

1

9

I need to run a method with a given parameter in a thread. I've noticed that when I run it, the parameter is wrong. For the example given, I have an array int[] output with the numbers 1-7. For each number, I create a thread with the method WriteInt(i). I expect the output to be 1-7 in any order, but I consistently see some numbers missed and others duplicated. What is going on and what would the correct way be to start these threads?

(The list is only there to join the threads afterwards)

class Program
{
    static void Main(string[] args)
    {
        int[] output = { 1, 2, 3, 4, 5, 6, 7 };

        List<Thread> runningThreads = new List<Thread>();

        foreach (int i in output)
        {
            Thread thread = new Thread(() => WriteInt(i));
            thread.Start();
            runningThreads.Add(thread);
        }
        foreach(Thread t in runningThreads)
        {
            t.Join();
        }
    }

    private static void WriteInt(int i)
    {
        Console.WriteLine(i);
    }
}

Example output:

3
3
4
5
6
7
Diecious answered 7/9, 2012 at 19:25 Comment(2)
try parallel foreach. That way you wont create more thread and end up performance threat.Burse
Unfortunately we are stuck on .net 3.5 which doesn't have the parallel foreach.Diecious
G
15

The closure created by the lambda (() => WriteInt(i)) is getting closing over the variable i, not the value set to i within each iteration. As the thread runs, it uses the value set within i at that point in time, which is likely already been changed due to the foreach loop processing.

You need a temporary:

foreach (int i in output)
{
        int temp = i;
        Thread thread = new Thread(() => WriteInt(temp));
        thread.Start();
        runningThreads.Add(thread);
}

For details on what's happening, see Eric Lippert's post titled Closing over the loop variable considered harmful.

Also, in C# 5 (VS2012), this is no longer an issue for foreach loops. It will still happen with a for loop, however.

Gothicism answered 7/9, 2012 at 19:26 Comment(1)
+1. An alternative to the temp variable is to pass i as a parameter using the ParameterizedThreadStart constructor.Xanthochroism

© 2022 - 2024 — McMap. All rights reserved.