How to use C ++ to easily implement thread control-2

Source: Internet
Author: User

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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.