本文介绍了如何将类成员函数传递给pthread_create(....)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好:


我想将一个类成员函数传递给pthread_create,我的

代码如下,但我不知道我不知道怎么把

myobj.thread_function传递给pthread_create函数。


#include< pthread.h>

class测试

{

public:

test(){}

~test(){}

void thread_function(void *){}

};


int main()

{

test myobj;


pthread_t thrd;


pthread_attr_t attr;

pthread_attr_init(& attr);

pthread_attr_setdetachstate(& attr,PTHREAD_CREATE_DETACHED);


/ ************ ************************************** ************ /

/ *如何将myobj.thread_function(NULL)传递给pthread_create(...)* /

pthread_create(& thrd,& attr,/ *? ??????? * /,NULL);

/ **************************** ********************** ************ /


pthread_attr_destroy(& attr);


}


pthread_create(...)'s's语法


#include< pthread.h> ;;


int pthread_create(pthread_t * thread,const pthread_attr_t * attr,void

*(* start_routine)(void *),void * arg);

Hi all:

I want to pass a class-member function to pthread_create, and my
code is in the following, but I don''t know how to pass
myobj.thread_function to pthread_create function.

#include <pthread.h>
class test
{
public:
test(){}
~test(){}
void thread_function(void*){}

};

int main()
{
test myobj;

pthread_t thrd;

pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);

/************************************************** ************/
/* How to pass myobj.thread_function(NULL) to pthread_create(...)*/
pthread_create(&thrd, &attr, /*????????*/, NULL);
/************************************************** ************/

pthread_attr_destroy(&attr);

}

the pthread_create(...)''s Syntax

#include <pthread.h>;

int pthread_create (pthread_t *thread, const pthread_attr_t *attr, void
*(*start_routine) (void *), void *arg) ;

推荐答案




[]


请参阅pthread_create()接受回调函数指针和用户void *

指针,该指针可用于将指针传递给对象。所有你需要做的就是使用小thunk作为线程启动例程,将

控制流引导到对象的成员函数中。


#include< pthread.h>


班级考试

{

public:

test(){}

~test(){}

void thread_function(){}


};


模板< class T,void(T :: * mem_fn)()>

void * thunk(void * p)

{

(static_cast< T *>(p) - > * mem_fn)();

返回0;

}


int main()

{

test myobj;


pthread_t thrd;


pthread_attr_t attr;

pthread_attr_init(& attr);

pthread_attr_setdetachstate(& attr,PTHREAD_CREATE_DETACHED) ;


pthread_create(& thrd,& attr,thunk< test,& test :: thread_function>,

& myobj);


pthread_attr_destroy(& attr);

}


-

Maxim Yegorushkin

< fi *************** *@gmail.com>



[]

See, pthread_create() takes a callback function pointer and a user void*
pointer which can be used for passing a pointer to an object. All you have
to do is to use little thunk as a thread start routine that directs the
control flow into a member function of the object.

#include <pthread.h>

class test
{
public:
test(){}
~test(){}
void thread_function(){}

};

template<class T, void(T::*mem_fn)()>
void* thunk(void* p)
{
(static_cast<T*>(p)->*mem_fn)();
return 0;
}

int main()
{
test myobj;

pthread_t thrd;

pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);

pthread_create(&thrd, &attr, thunk<test, &test::thread_function>,
&myobj);

pthread_attr_destroy(&attr);
}

--
Maxim Yegorushkin
<fi****************@gmail.com>




你绝对不能!


C ++成员函数不是externC。

pthread_create所需的功能。


我有不同的链接,它也有一个隐藏的参数,所以

签名是错误的。


有些环境可以让你逃脱静态会员,但这个

是不便携的。


最好的办法是将方法称为public,并将该类的

实例作为arg参数传递。然后start_routine可以使用
投射它的参数并调用该成员。


别忘了将start_routine声明为externC。 。


Ian


You absolutely can''t!

A C++ member function is not an extern "C" function as required for
pthread_create.

It my have different linkage and it also has a hidden this parameter, so
the signature is wrong.

Some environments will let you get away with a static member, but this
isn''t portable.

Your best bet is to have the method to be called public and pass an
instance of the class as the arg parameter. The start_routine can then
cast it''s parameter and call the member.

Don''t forget to declare the start_routine as extern "C".

Ian




[]

请参阅,pthread_create()接受回调函数指针和用户
void *指针,可用于将指针传递给对象。所有你需要做的就是使用小thunk作为线程启动例程,将控制流引导到对象的成员函数中。

#include< pthread。 h>

班级测试
公开:
test(){}
~test(){}
void thread_function() {}

};

模板< class T,void(T :: * mem_fn)()>
void * thunk(void * p)
{
(static_cast< T *>(p) - > * mem_fn)();
返回0;
}


[]

See, pthread_create() takes a callback function pointer and a user
void* pointer which can be used for passing a pointer to an object. All
you have to do is to use little thunk as a thread start routine that
directs the control flow into a member function of the object.

#include <pthread.h>

class test
{
public:
test(){}
~test(){}
void thread_function(){}

};

template<class T, void(T::*mem_fn)()>
void* thunk(void* p)
{
(static_cast<T*>(p)->*mem_fn)();
return 0;
}



但是模板不能是pthread_create所要求的外部C。


Ian


But a template can''t be extern "C", as required by pthread_create.

Ian


这篇关于如何将类成员函数传递给pthread_create(....)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 18:15