Class member function as pthread_create function parameter

Source: Internet
Author: User

From:http://www.cnblogs.com/shijingxiang/articles/5389294.html

Recently, the thread pool needs to be encapsulated into a C + + class named ThreadPool. Call Pthread_create in the member function Exec_task of the class to start the thread execution routine thread_rounter. After compiling the error is as follows:

Spfs_threadpool.cpp:In member function ' int threadpool::exec_task (task*) ':

Spfs_threadpool.cpp:174:error:argument of 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. 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.

In conclusion, my problem can be solved in this way.

Code before the problem:

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

{

Direct access to members of a class

}

Called in the Exec_task function:

Pthread_create (&tid,null,thread_rounter,null);//start thread execution routines

The code to fix the problem:

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

{

Threadpool *p= (Threadpool *) tmp;

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

}

Called in the Exec_task function:

Pthread_create (&tid,null,thread_rounter, (void *) this);//start thread execution routines

--------------------------------------------------------------------------------------------------------------- -------

Search on the Internet there are other solutions, excerpts from the following, in order to show respect to mark the source of the article, thank the original author.

Scenario Two:

Declare the thread initiation function as a template function.

Excerpt from: HTTP://HI.BAIDU.COM/ANGELEVIL2006/ITEM/E1806EC30574FF11515058D1

Template <typename TYPE,void(TYPE::* _runthread) () >void* _THREAD_T (void* param)//thread start function, declared as a template function{TYPE* this = (type*) param; this-_runthread (); returnNULL; }      classMyClass { Public: MyClass (); void_runthread (); Private: pthread_t tid;      }; voidMyclass::_runthread () { This-dosomething (); } myclass::myclass () {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. 

Take this scenario into my project:

Code before the problem:

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

{

Direct access to members of a class

}

Called in the Exec_task function:

Pthread_create (&tid,null,thread_rounter,null);//start thread execution routines

The code to fix the problem:

To add a public member function:

void Run ()

{

This function replaces the execution code of the above Thread_rounter

}

Thread_rounter Modify to global template function:

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

void * Thread_rounter (void * param)

{

TYPE *p= (type*) param;

P->run ();

return NULL;

}

Called in the Exec_task function:

Pthread_create (&tid,null,thread_rounter<threadpool,&threadpool::run>, (void *) this);

Summarize:

The key to solving this problem is to find ways to make the start function pointer satisfy the void* (*) (void *) type.

Overwriting a startup function with a static member function is appropriate for situations where the source code of a class can be modified.

And the start function as a template function can also be applied to the absence of the source code of the case, write a class, the public inherit from the original class, add the Startup function as a template function.

Class member function as pthread_create function parameter

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.