c++类中线程创建问题

killgxlin 2007-12-18 06:30:19

#include <iostream>
#include <pthread.h>
using namespace std;
class abc{
public:
abc(){
pthread_t tid;
pthread_create(&tid,NULL,mythread,NULL);
};
void *mythread(void *arg){
cout<<"hello"<<endl;
}
}
int main(){
abc myabc();
return 0;
}

编译不通过,说是传递给pthread_create的mythread参数不匹配。把mythread声明为static就可以了,但是我想用对象中的变量和函数,有没有其他方法,多谢各位大侠
...全文
132 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
maxx 2007-12-19
  • 打赏
  • 举报
回复
#include <iostream>
#include <pthread.h>
using namespace std;
class abc{
public:
abc(){
pthread_t tid;
pthread_create(&tid,NULL,mythread,this); // 传this
};
static void *mythread(void *arg){ // 加static
cout<<"hello"<<endl;
}
};
int main(){
abc myabc();
return 0;
}
Wolf0403 2007-12-19
  • 打赏
  • 举报
回复
接口要求: C-linkage,单一函数
C++ 类成员函数的链接接口不符合。
kittyhuaner 2007-12-19
  • 打赏
  • 举报
回复

#include <iostream>
#include <pthread.h>
using namespace std;
class abc{
public:
abc(){
pthread_t tid;
pthread_create(&tid,NULL,mythread,this); // 传this
};
static void *mythread(void *arg){ // 加static
(abc*)arg->threadFunc();
}
void threadFunc()
{
cout < <"hello" < <endl;
}

};
int main(){
abc myabc();
myabc.threadFunc();
return 0;
}

ckt 2007-12-18
  • 打赏
  • 举报
回复
你可以将入口函数声明为类友元
然后把类对象指针作为参数传入
NaiNaiGeXiong 2007-12-18
  • 打赏
  • 举报
回复
是先有鸡还是先有鸡蛋?

64,642

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧