member functions are called as thread functions

Source: Internet
Author: User

Problems encountered

In programming we need to encapsulate the data into a class, calling Pthread_create to use member functions to create a thread that is often unsuccessful!

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

There is a type mismatch problem. Because pthread_create requires a parameter type of void* (*) (void*), and Thread_rounter is a member function of a class, its type is void* (Threadpool::) (void*) member function pointer. We know that the member functions of a class will become a global function with the this pointer parameter after being processed by the compiler, so the type is doomed to be mismatched.

Solution Solutions

Despair, especially for the super-language of C + +, known as the Wenger Giant Knife (http://coolshell.cn/articles/6639.html#more-6639), is sure to have a variety of kinky tricks to provide an unimaginable solution.


A mocking image of various programming languages

Declaring a static member function as a thread function

However, if Thread_rounter is declared as a static type, the compiler converts the static form of a function into a global function without the this pointer, so its type can match the type of parameter that Pthread_create requires. However, a static member function of a class cannot access a non-static member of a class, but this can be resolved by passing the this pointer.

Staticvoid*threadfun (void*)

{

DoSomething

}

Callback member functions with global functions

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

{

classname*p= (ClassName *) tmp;

Indirect access to non-static members of a class through the P pointer

}

Ultimate Solution C + + templates

Declaration defines a template function

Template <typename type, void (type::* _runthread) () >

void* _thread_t (void* param)

{

type* this = (type*) param;

This->_runthread ();

return NULL;

}

/*

_thread_t: As a template function name, you can start at random

Type: Class name

_ Runthread: The name of the member function in the type class, which must be pbulic. Can not be arbitrarily named. The return type and parameters must be consistent with the class.

*/

Class MyClass

{

Public

MyClass ();

void _runthread ();

Private

pthread_t Tid;

};

void Myclass::_runthread ()

{

This->dosomething ();

//...

}

Myclass::myclass ()

{

Use the powerful template function of C + + to implement the function transformation skillfully

Pthread_create (&tid, NULL, _thread_t<myclass,&myclass::_runthread>, this);

}

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

Note: 1, the name can only be _runthread, can not be modified when the template parameters are specified;

2, _runthread can only be public, unless the _thread_t is defined to the internal MyClass.

Summarize

These kinky tricks are not necessary to remember, when used can be remembered to find out. The above-mentioned work has only one purpose, converting the function type to void* (*) (void*) type. Similarly, this technique can be transplanted to other places of need, such as functions that convert member functions to T (*) (t*), which are not explained here.

If the reader see here does not understand what the above mean or a little dizzy, no relationship, Tiger use on it. Readers are advised to read the new C + + teaching

member functions are called as thread functions

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.