A member function is called as a thread function. A member function is called by a thread.

Source: Internet
Author: User

A member function is called as a thread function. A member function is called by a thread.
Problems encountered

In programming, We Need To encapsulate data into a class. Calling pthread_create to create a thread using the member function is often unsuccessful!

Error: argumentof type 'void * (Threadpool: :) (void *) 'does not match 'void * (*) (void *)'

Type Mismatch occurs. Because the parameter type required by pthread_create is void * (*) (void *), and when thread_rounter acts as a class member function, its type is void * (Threadpool: :) (void *) member function pointer. We know that the class member function will become a global function with the this pointer parameter after being processed by the compiler, so the type is not necessarily matched.

Solution

The path to perfection, especially for C ++, a super language known as the Wenger giant knife (http://coolshell.cn/articles/6639.html#more-6639), will surely have a variety of odd sex techniques to provide an incredible solution.


Ridicule of various programming languages

Declare static member functions as thread Functions

However, if thread_rounter is declared as the static type, the compiler converts a static function to a global function without the this pointer, so its type can match the parameter type required by pthread_create. However, the static member function of the class cannot invoke non-static members of the class, but this problem can be solved by passing the this pointer.

Staticvoid * threadfun (void *)

{

// Dosomething

}

Call back a member function through a global function.

Void * thread (void * tmp)/thread execution function

{

ClassName * p = (ClassName *) tmp;

// Indirect callback class non-static members through the p pointer

}

The ultimate solution C ++ template from below (http://hi.baidu.com/angelevil2006/item/e1806ec30574ff11515058d1)

// Declare and define a template function

Template <typename TYPE, void (TYPE: * _ RunThread) ()>

Void * _ thread_t (void * param)

{

TYPE * This = (TYPE *) param;

This-> _ RunThread ();

Return NULL;

}

/*

_ Thread_t: name of the template function.

TYPE: Class Name

_ RunThread: name of the member function in the TYPE class. It must be pbulic. You cannot name it at will. The return type and parameters must be consistent with those in the class.

*/

Class MyClass

{

Public:

MyClass ();

Void _ RunThread ();

Private:

Pthread_t tid;

};

 

Void MyClass: _ RunThread ()

{

This-> DoSomeThing ();

//...

}

 

MyClass: MyClass ()

{

// Use the powerful C ++ template function to implement Function Conversion.

Pthread_create (& tid, NULL, _ thread_t <MyClass, & MyClass: _ RunThread>, this );

}

 

The function template can not only replace the type itself, but also replace the member functions of the type.

Note: 1. the name can only be _ RunThread and cannot be modified when the template parameters are specified;

2. _ RunThread can only be public unless _ thread_t is defined inside MyClass.

Summary

There is no need to remember these odd sex techniques. You can remember to find them when you use them. The preceding work has only one purpose, and converts the function type to the void * (*) (void *) type. Similarly, this technique can be transplanted to other needs. For example, if you can convert a member function into a T (*) (T *) function, we will not explain it here.

If the reader does not understand what the above is, or is a little dizzy, it does not matter. We recommend that you read C ++ again.
How to call a class member function within a thread

1. Upload the object pointer to the thread as a parameter.
2. Compile the object and call it everywhere.

In VC, how does a multi-thread call a member function?

When creating a thread, do you still remember to have a parameter that needs to be passed in? After this is passed in, it is retrieved inside the thread. Pay attention to the type conversion. this is obtained, use everything with you

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.