How to define a thread function in a C ++ class

Source: Internet
Author: User

Yesterday, I used C ++ to encapsulate a game function that I wrote. One small problem is that two thread functions must be defined in the encapsulated C ++ class, for example:
1. class
2 .{
3. public:
4. ();
5 .~ A ();
6.
7. void XXX (void * p); // This function is a thread callback function.
8 .};
XXX is the thread callback function, which must be called by the _ beginthread () function in another thread startup function. But when I write this:
_ Beginthread (XXX, 0, NULL );
The compiler reports an error .. That is, compilation fails. I thought about it carefully and thought it was indeed impossible to compile it, because the thread function must know the function entry address during compilation, and I write it like this, the XXX portal address is indeed unknown ..
So I think of two methods to change the unknown to known. One is to define XXX as a static member function, as shown below:
Static void XXX (void * p );
The other is to define XXX as a global function.
In fact, it is to let _ beginthread () Know the entry address of this function. Global functions and static functions are stored in one place and are known during compilation.
When I thought everything was okay, I found another error in the XXX () function ..
It indicates that I have used a member that is not in the category. This is actually the meaning:
1. class
2 .{
3. public:
4. ();
5 .~ A ();
6.
7. static void XXX (void * p); // This function is a thread callback function www.2cto.com
8.
9. int a; // It will be modified in the XXX Function
10 .};
The member variable A of a will be modified in the XXX () function, but it cannot be seen in the XXX function.
You may ask, why can't you see it? This is what I usually write. For example, other member functions defined in A can directly access.
Indeed, other common member functions in Class A can access this member variable. However, it is easy to ignore that there is actually a hidden parameter in the general member function, which is the this pointer to this class. we access the member variable a in the member function, it is actually accessing this->. So our member function can find this a, but our thread function does not pass this parameter. XXX () is created using the _ beginthread () function, so if we want to pass a parameter to XXX (), we must write it in the third parameter of _ beginthread (). We can see that the third parameter I wrote above is NULL, therefore, member variable a cannot be accessed in XXX (), because this pointer is not available.
After understanding this, it will be very easy. In _ beginthread (), pass the this pointer to the XXX () function, and then in XXX () in the function, convert the void * type to the pointer of Class. As follows:
1. class
2 .{
3. public:
4. ();
5 .~ A ();
6.
7. static void XXX (void * p); // This function is a thread callback function.
8. void start (); // thread start Function
9. int a; // It will be modified in the XXX Function
10 .};
11.
12. void start ()
13 .{
14. _ beginthread (XXX, 0, this );
15 .}
16.
17. void XXX (void * p)
18 .{
19. A * handle = (A *) p;
20. int B = handle->;
21 .}
Am I a good guy... Haha. Okay, I am flooded.

From cainiao surfaced
 

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.