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:
ThreadTest1 test1;
TThread<ThreadTest1> thTest1(test1,&ThreadTest1::InsideThread);
thTest1.StartAndWait();
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;
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