By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,899 Members | 1,230 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,899 IT Pros & Developers. It's quick & easy.

Getting garbled data into thread

P: 45
Hello Everybody,

I have this following code with me:
Expand|Select|Wrap|Line Numbers
  1. 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. }
  34.  

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:

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......

Zack
Jun 17 '13 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.