Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Template Thread Library

0.00/5 (No votes)
26 Feb 2003 1  
Allows any class method to be executed in a separate thread without global functions.

Introduction

This project demonstrate two non MFC thread classes: one of them is a template library that can make any member function of any class executed in different thread, without requiring that you define any static or global functions; the other class requires inheritance in the same way that CWinThread under MFC work 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 parameters and return void as 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 have a single member function. To run this member in a different thread use the TThread class as follow:

// First create an instance of the class that has the required member
// function
ThreadTest1 test1; TThread<ThreadTest1> thTest1(test1,&ThreadTest1::InsideThread); // The constructor takes 2 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. Wait until the thread terminates; optionally you can specify the number of milliseconds to wait. thTest1.WaitUntilTerminate();

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

TThread
The Constructor take 3 parameters, the first is an instance of a class that contain the member function that we want to run it in 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
Wait until the thread is terminate also you can define how many milli second 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
Start the thread and return immediatly.
StartAndWait
Start the thread and wait until it begin executing.
Pause
Suspend the thread, you need to call Start again to resume the thread.
IsRunning
Return true if the thread running.
IsTerminated
Return true if the thread is terminated.
IsSuspend
Return true if the thread is suspended.
SetPriority and GetPriority
Set the priority to a specific value, or get the current priority of the thread, the thread must be in running mode before calling any of these members.
SpeedUp
Increase the thread priority to one step, the thread must be in running mode before calling any of these members.
SlowDown
Decrease the thread priority to one step, the thread must be in running mode before calling any of these members.
Terminate
Terminate the thread immediatly, Not Safe!!!.

Second, in the CThread class, which is like MFC's 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 Start() or the StartAndWait() method:

ThreadTest2 test2;



// This member actually starts the thread and waits until the thread begins
// execution






test2.StartAndWait();


// Wait until the thread terminates; optionally you can specify the number
// of milliseconds to wait


test2.WaitUntilTerminate();
The other members of CThread are exactly like TThread.

Note: To pass any parameters to the thread, you just need to define a member variable in the thread class before calling the Start() or StartAndWait() member functions, 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 techniques.

History

  • Date Posted: February 19, 2003

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here