Contents

  Threads
  Race Conditions
  Locking
  Dead Locks
  Live Locks
  Starvation
  Affinity
Threads - Introduction

A thread of simply an instruction to the underlying operating system to asynchronously start a function, optionally with parameters. In some languages, the thread will additionally be an object. That's it. Thats threading.

Now though, the fun (and pain) begins. But before we get to that, lets see how to create and start a thread in WIN32, .Net and Java.

In .Net, we create a new Thread instance and pass it a delegate (there are other paramatized delegates, but I will not go into those).

C#
    class MyClass
    {
        private Thread _thread
        
        private void MyThreadStartMethod(){...}
        
        private void StartThread()
        {
            _thread = new Thread(new ThreadStart(MyThreadStartMethod));
            _thread.Name = "An Appropriate Name"; // Good Practice
            _thread.start()
        }
    }
In Java we have 2 ways to create a thread. The *easy* (but not recommended) way to create a thread is to derive from the thread class and override the run method. The beat way is to create a class that implements Runnable (an interface with a single parameterless method called run() ) and pass that to a new instance of a Thread instance. Both ways then require the start method to be called.

Note (to advanced users): I am not going in to Callables and Futures here on purpose. I am trying to get accross the concepts.

Java
    class MyThreadClass extends Thread
    {
        public void run() { . . . }
    }
    
    class MyRunnableClass implements Runnable
    {
        public void run() { . . . }
    }
    
    class MyTestClass
    {
        private MyThreadClass _thread1;
        private Thread        _thread2;
       
        private void FirstWay()
        {
            _thread1 = new MyThreadClass();
            _thread1.setName("An Appropriate Name"); // Good Practice
            _thread1.start()
        }
        
        private void SecondWay()
        {
            _thread2 = new Thread(new MyRunnableClass());
            _thread2.setName("An Appropriate Name"); // Good Practice
            _thread2.start()
        }
    }
In WIN32 A thread is not an object, but a handle. There are also 3 ways to create a thread, namely;

  • CreateThread
  • _beginthread
  • _beginthreadex
All of these involve the creation of 2 handles; one for the thread to own and use, and one for you.
However,

  • CreateThread does not start the C runtime and it requires extra cleanup code for multi-threaded use
  • _beginthread has a race condition (discussed later) as the first thing _beginthread does is to close its handle to the thread, so by the time your handle is returned from the function, the thread might have exited, making your handle invalid.
  • Thus we always use _beginthreadex


Win32
   
    private void CreateAndStartAThread()
    {
        unsigned threadId;
        uintptr_t hThread = _beginthreadex(
                                NULL,       // Ignoring Security
                                0   ,       // Default Stack Size
                                &ThreadProc,// Function to call
                                NULL,       // void* Arg List
                                0,          // 0 for running or CREATE_SUSPENDED for suspended
                                &threadId   // Thread ID from OS
                                
        CloseHandle(hThread);// Remember you MUST close this when you are done with this thread
    }
    
    private static unsigned ThreadProc(void* pArgs)
    {
        // Must be static to avoid the 'this' pointer becoming a parameter
    }
    
Next >>


 
 
 
 
 

Privacy Policy
©2008 DebugInspector. All rights reserved