In the previous article, I wrote several key points for a good thread control class and provided interfaces for the thread control class. The careful program shows that it is impossible to easily inherit the above interface and declare a thread object, nor can it call the member functions of the thread class. In the same class, there can be several threads for different jobs and different member functions can be called. Therefore, you need to write a piece of code to implement a thread function that inherits the class and is easy to call the Thread class.
Here, I will not sell my secrets. I will write out the inherited classes I have written first.
//---------------------------------------------------------------------------
# Define CAsThread (classname, vFunc )\
Class classname # vFunc: public _ CAsThread \
{\
Protected :\
Void WorkThread (void )\
{\
If (0! = M_Owner )\
M_Owner-> vFunc ();\
}\
Public :\
Classname * m_Owner ;\
};\
Classname # vFunc
//---------------------------------------------------------------------------
This inheritance class is written in macros. In this way, after macro expansion in the program compiler, the thread inheritance class can be linked with the Thread class and member functions. I will briefly describe a macro implementation function. This macro implements A Class, which consists of A Class Name (A) and A function (B). This class inherits _ CAsThread and implements WorkThread, A member function B of A is called in the WorkThread function to call the member function of the class. For example
Class CTestThread
{
Public:
Void Test () // thread member function
{
Printf ("Helllo TestThread \ n ");
Sleep (1000 );
}
Public:
CAsThread (CTestThread, Test) m_Thread;
};
This is what this testing Thread class looks like after a macro Conference
Class CTestThread
{
Public:
Void Test () // thread member function
{
Printf ("Helllo TestThread \ n ");
Sleep (1000 );
}
Public:
Class CTestThreadTest
{
Protected:
Void WorkThread (void)
{
If (0! = M_Owner)
M_Owner-> vFunc ();
}
Public:
CTestThread * m_Owner;
};
CTestThreadTest m_Thread;
};
The preceding macro allows you to easily create thread classes and thread objects and call member functions in the class.
This article is from the "Amu Xue" blog and will not be reproduced!