locked
Getting garbled data into thread RRS feed

  • Question

  • Hello Everybody,

    I have this following code with me:

    public class Program
     2. {
     3.    static int numThreads = 10;
     4.    static ManualResetEvent resetEvent = new ManualResetEvent(false);
     5.    static int toProcess = numThreads;
     6. 
     7.    static void Main(string[] args)
     8.    {
     9.        // Start workers.
     10.        for (int i = 0; i < numThreads; i++)
     11.        {
     12.           new Thread(() => MyFunction(i)).Start();
     13.        }
     14. 
     15.        // Wait for workers.
     16.        resetEvent.WaitOne();
     17.        Console.WriteLine("All threads finished.");
     18.        Console.ReadLine();
     19.    }
     20. 
     21.    public static void MyFunction(int i)
     22.    {
     23.       Console.WriteLine("Thread " + (i) + "  started......., sleeping for " + (i * 1000) + " seconds");
     24. 
     25.       Thread.Sleep(i * 1000);
     26. 
     27.       Console.WriteLine("Thread " + (i ) + " ended.......");
     28. 
     29.       // If we're the last thread, signal
     30.       if (Interlocked.Decrement(ref toProcess) == 0)
     31.          resetEvent.Set();
     32.    }
     33. }
    

    When I execute this code, instead of getting messages like this:

    Thread 1 started......., sleeping for 1000 seconds
    Thread 2 started......., sleeping for 2000 seconds
    Thread 3 started......., sleeping for 3000 seconds
    Thread 4 started......., sleeping for 4000 seconds
    .
    .
    .
    Thread 10 started......., sleeping for 10000 seconds
    Thread 1 ended.......
    Thread 2 ended.......
    Thread 3 ended.......
    Thread 4 ended.......
    .
    .
    .
    Thread 10 ended.......
    All threads finished.
     
    I get messages like this:

    Thread 2 started......., sleeping for 2000 seconds
    Thread 3 started......., sleeping for 3000 seconds
    Thread 3 started......., sleeping for 3000 seconds
    Thread 4 started......., sleeping for 4000 seconds
    Thread 9 started......., sleeping for 9000 seconds
    Thread 9 started......., sleeping for 9000 seconds
    Thread 9 started......., sleeping for 9000 seconds
    Thread 9 started......., sleeping for 9000 seconds
    Thread 10 started......., sleeping for 10000 seconds
    Thread 10 started......., sleeping for 10000 seconds
    Thread 2 ended.......
    Thread 3 ended.......
    Thread 3 ended.......
    Thread 4 ended.......
    Thread 9 ended.......
    Thread 9 ended.......
    Thread 9 ended.......
    Thread 9 ended.......
    Thread 10 ended.......
    Thread 10 ended.......
    All threads finished.


    Could you please tell me where I am making mistake...

    If the code is write, does this mean that when we create thread, data doesn't get passed correctly & it should be sent by some other mechanism....

    your help is really appreciated........

    Thanks in advance......
    Abhijeet

     

    Tuesday, June 25, 2013 1:10 PM

Answers