pthread_create 错误,怎么定义class类中的函数指针

来源:百度知道 编辑:UC知道 时间:2024/05/29 04:59:51
class Identity
{
public :
Identity();
void *Shown(void * arg);
void *RunExec(void * arg);
private:
int a;
};
Identity:: Identity()
{
pthread_t pthread1,pthread2;
pthread_create(&pthread1, NULL,Shown, NULL);
pthread_create(&pthread2, NULL, RunExec, NULL);

pthread_join(pthread1,NULL);
pthread_join(pthread2,NULL);
}
void * Identity::Shown(void * arg)//->这里应该怎么定义,是否正确
{
a=1;
printf("pthread1--->a=%d\n",a);
}
void *Identity::RunExec(void * arg)//->这里应该怎么定义,是否正确

{
a=2;
printf("pthread2--->a=%d\n",a);

pthread_exit(0);
}
int main(void)
{
Identity b;
return 0;
}
运行程序显示这两个子函数不匹配,以下是错误信息:
pthread.cpp:17: no matches converting function `Shown' to type `void*(*)(void*)
'
pthread.cpp:9: candidates are: void* Identity::Shown(void*)
pthr

虽然不知道pthread是什么。。不过大致了解是什么问题了。pthread_create函数的第三个参数要的函数指针,不是类的成员函数指针,这两个是不一样的。你只能传普通的非成员函数指针进去,也就是把你那些show啊,runexec啊,定义为普通的函数,别定义为类的成员函数