Template Thread Library



Click here for a larger image.

Environment: Visual C++ 7.0, Windows XP sp1, Windows 2000 sp3

Abstract

This project demonstrates two non-MFC thread classes. One of them is a template library that can make any member function of any class execute in different threads, without the requirement of defining any static or global function. The other class requires inheritance in the same way that CWinThread under MFC works, but without any MFC dependency. Both classes use “_beginthreadex”.

Details

First, the Template thread library takes in the constructor—a pointer to a class—and a point to one of its member functions. The member function must take no parameter and return void, as in the following:

class ThreadTest1
{
public:
  void InsideThread()
  {
    for(int i=0;i<5;i++)
    {
      cout << "ThreadTest1: " << i << endl;
    }
  }
};

This is a very simple class that has a single member function. To run this member in a different thread, use the TThread class, as follows:

ThreadTest1 test1;    //-- First, create an instance of the class
                      // that has the required member function --

  TThread<ThreadTest1> thTest1(test1,&ThreadTest1::InsideThread);
//-- The constructor takes two parameters and a third optional
// parameter; the first is an object of the class and the second
// is pointer to one of its member functions. The third optional
// parameter is the priority of the thread; the default is
// Normal --
  thTest1.StartAndWait();    //-- This member actually starts
                             // the thread and waits until the
                             // thread begins execution --
  thTest1.WaitUntilTerminate();    //-- Waits until the thread
                                   // terminates; optionally,
                                   // you can specify the number
                                   // of msec to wait --

The TThread class has a lot of member functions to increase and decrease the priority, terminate the thread immediately, and check the status of the thread.

Function Description
TThread The Constructor take three parameters. The first is an instance of a class that contains the member function that we want to run in a different thread. The second parameter is a pointer to the member function. The third one, which is optional, is the priority of the thread; the default is Normal.
WaitUntilTerminate Waits until the thread is terminated; also, you can define how many milliseconds to wait. The default is infinite. This member doesn’t terminate the thread; if the thread didn’t terminate at the specified number of msec, it will return false; otherwise, it will return true.
Start Starts the thread and returns immediately.
StartAndWait Starts the thread and waits until it begins executing.
Pause Suspends the thread. You need to call Start again to resume the thread.
IsRunning Returns true if the thread is running.
IsTerminated Returns true if the thread is terminated.
IsSuspend Returns true if the thread is suspended.
SetPriority and GetPriority Sets the priority to a specific value, or gets the current priority of the thread. The thread must be in running mode before calling any of these members.
SpeedUp Increases the thread priority to one step. The thread must be in running mode before calling any of these members.
SlowDown Decreases the thread priority to one step. The thread must be in running mode before calling any of these members.
Terminate Terminates the thread immediatly. Not Safe!!!

Second is the CThread class, which is a MFC-like CWinThread. You must overload the OnRunning method:

class ThreadTest2:public CThread
{
public:
  void OnRunning()
  {
    for(int i=0;i<5;i++)
    {
      cout << "ThreadTest2: " << i << endl;
    }
  }
};

Just create an instance of this class and call the Start() or the StartAndWait() method:

ThreadTest2 test2;

  test2.StartAndWait();    //-- This member actually starts the
                           // thread and waits until the thread
                           // begins execution --
  test2.WaitUntilTerminate();    //-- Waits until the thread
                                 // terminates; optionally, you
                                 // can specify the number of
                                 // msec to wait --

The other members of CThread are exactly like TThread.

Note: To pass any parameter to the thread, you just need to define a member variable to the thread class before calling the Start or StartAndWait member function, and you can use this variable inside the thread. Just make sure that no two threads access the same variable at the same time by using any locking technique.

Downloads

Download demo project – 130 Kb

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read