CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Multi-Threading

  1. #1
    Join Date
    Jun 2005
    Location
    Turkey
    Posts
    54

    Multi-Threading

    Code:
    using System;
    using System.Threading;
    
    public class Test
    {
        static void Main()
        {
            ThreadStart job = new ThreadStart(ThreadJob);
            Thread thread = new Thread(job);
            thread.Start();
            
            for (int i=0; i < 5; i++)
            {
                Console.WriteLine ("Main thread: {0}", i);
                Thread.Sleep(1000);
            }
        }
        
        static void ThreadJob()
        {
            for (int i=0; i < 10; i++)
            {
                Console.WriteLine ("Other thread: {0}", i);
                Thread.Sleep(500);
            }
        }
    }
    I found this code in an article I read. The output is:
    Code:
    Main thread: 0
    Other thread: 0
    Other thread: 1
    Main thread: 1
    Other thread: 2
    Other thread: 3
    Main thread: 2
    Other thread: 4
    Other thread: 5
    Main thread: 3
    Other thread: 6
    Other thread: 7
    Main thread: 4
    Other thread: 8
    Other thread: 9
    The thing that i dont understand is why the first output is main thread, not other thread. Can anyone explain?

  2. #2
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: Multi-Threading

    The whole point about threading is that you can't assume that one bit of code is called before another. I.e. it's not deterministic.

    It entirely depends on the scheduling in the processor of each particular thread : taking your code as an example you might find that on a different machine that the thread code IS called before your main thread's code.

    That's the purpose of the synchronisation methods : lock, events etc etc. They are there so you can decide that a piece of code will always be run before another piece of code, or that a single piece of code is only ever run by one thread at a time.

    This is what makes them so complicated. Personally I'd steer clear of them if you don't really need to use them. You can cause all sorts of problems for yourself if you liberally start throwing them about "because it looks like a good idea."

    If you needed to ensure that the thread code gets called first before the main code you'd have to use an event to signal that the main code can continue like this :

    Code:
    public class Test
    {
        static AutoResetEvent m_eventStarted = new AutoResetEvent(false);
    
        static void Main()
        {
            ThreadStart job = new ThreadStart(ThreadJob);
            Thread thread = new Thread(job);
            thread.Start();
    
    	m_eventStarted.WaitOne();
            
            for (int i=0; i < 5; i++)
            {
                Console.WriteLine ("Main thread: {0}", i);
                Thread.Sleep(1000);
            }
        }
        
        static void ThreadJob()
        {
            for (int i=0; i < 10; i++)
            {
                Console.WriteLine ("Other thread: {0}", i);
                Thread.Sleep(500);
    
                // if we've outputted the first, set the event to signal that main thread can continue
                if (i == 0)
                {
                      m_eventStarted.Set();
                }		
            }
        }
    }
    Darwen.
    Last edited by darwen; August 4th, 2005 at 04:26 AM.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured