5

this is my app to excute a threading example, but the output is not as expected , anyone have any clue about that please

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace OTS_Performence_Test_tool
{
    class Program
    {

        static void testThread(string    xx)
        {

            int count = 0;
            while (count < 5)
            {
                Console.WriteLine(xx );
                count++;

            }
        }

        static void Main(string[] args)
        {
            Console.WriteLine("Hello to the this test app ---");

            for (int i = 1; i<=3; i++)
            {

                    Thread thread = new Thread(() => testThread("" + i + "__"));

                thread.Start();

            }


            Console.ReadKey();
        }
    }
}

but the out but is

3__

3__

3__

3__

3__

3__

3__

3__

3__

3__

4__

4__

4__

4__

4__

what happens exactly anyone can explain please thanks

Maadh
  • 643
  • 4
  • 24

3 Answers3

12

See Eric Lippert's excellent blog post on this issue.

This is being caused by access to a "modified closure".

Change your loop's body to this:

for (int i = 1; i<=3; i++)
{
    int j = i;  // Prevent use of modified closure.
    Thread thread = new Thread(() => testThread("" + j + "__"));

    thread.Start();
}

(Note that for a foreach loop, this was fixed in .Net 4.5, but it was NOT fixed for a for loop.)

Community
  • 1
  • 1
Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
4

Closure. You must pretty much copy the variable in the thread for it to keep the current value,.

Wight now all threads read the variable i with whatever value they have at the moment they run - NOT with the value it had when thread.start was called for them.

TomTom
  • 61,059
  • 10
  • 88
  • 148
1

testThread is not called when you create the Thread objects within the for loop - the method is called whenever the thread is scheduled to run. And that might happen later.

In your case, the threads started running after the for loop ended - by that time, i was equal to 3. So testThread was invoked 3 times with the value 3.

dcastro
  • 66,540
  • 21
  • 145
  • 155