免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 4851 | 回复: 13
打印 上一主题 下一主题

[函数] 怎样把pthread_create的线程函数当作类的成员函数编译通过? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2003-09-28 14:56 |只看该作者 |倒序浏览
在SOLARIS C++4。2下在一个类的成员里调用了一个线程,  
   
int myClass::start()
{
................
    thr_id = pthread_create(&thread_id, NULL, thread_method, NULL);
........................
}

但必须把thread_method声明如下:
void *thread_method(void *arg)

如果声明为void * myclass::thread_method(void *arg)
就编译不过,我想把它作为成员函数,怎么办???不然破坏封装性

论坛徽章:
0
2 [报告]
发表于 2003-09-29 09:04 |只看该作者

怎样把pthread_create的线程函数当作类的成员函数编译通过?

???

论坛徽章:
0
3 [报告]
发表于 2003-09-29 09:09 |只看该作者

怎样把pthread_create的线程函数当作类的成员函数编译通过?

看看linux c++ howto,上面对这个问题有详细的解答。

论坛徽章:
0
4 [报告]
发表于 2003-09-29 10:51 |只看该作者

怎样把pthread_create的线程函数当作类的成员函数编译通过?

我这没装linux呀,我用的是solaris,没找到相关文档

论坛徽章:
0
5 [报告]
发表于 2003-09-29 12:11 |只看该作者

怎样把pthread_create的线程函数当作类的成员函数编译通过?

你在myclass中定义了成员函数void *thread_method(void *arg)

就可以了,不用再在其它地方作专门的声明了,因为在其它成员函数调用此函数时,已经默认为该类的成员函数,相当于
int myClass::start()
{
thr_id = pthread_create(&thread_id, NULL, this->;thread_method, NULL);
........................

}

论坛徽章:
0
6 [报告]
发表于 2003-09-29 15:30 |只看该作者

怎样把pthread_create的线程函数当作类的成员函数编译通过?

错误!如下:
void * myclass::thread_method(void *arg)
pthread_create(&thread_id, NULL, this->;thread_method, NULL);



Error: Formal argument 3 of type void*(*)(void*) in call to pthread_create(unsigned*, const _pthread_attr*, void*(*)(void*), void*) is being passed void*(myclass::*)(void*).

论坛徽章:
0
7 [报告]
发表于 2003-09-29 16:01 |只看该作者

怎样把pthread_create的线程函数当作类的成员函数编译通过?

原帖由 "libad" 发表:
错误!如下:
void * myclass::thread_method(void *arg)
pthread_create(&thread_id, NULL, this->;thread_method, NULL);



Error: Formal argument 3 of type void*(*)(void*) in call to pthread_cre..........
   

你还没明白我的意思,
声明 void * myclass::thread_method(void *arg) 就不需要了,因为这语句已经在myclass的类定义中已经包含了.
在成员函数start()里面就按原来的调用就可以了
int myClass::start()
{
pthread_create(&thread_id, NULL, thread_method, NULL);
}

论坛徽章:
0
8 [报告]
发表于 2003-10-09 09:24 |只看该作者

怎样把pthread_create的线程函数当作类的成员函数编译通过?

不声明void * myclass::thread_method(void *arg) 了。错误一样呀。
我用的是solaris 8,c++4.2

论坛徽章:
0
9 [报告]
发表于 2003-10-09 09:49 |只看该作者

怎样把pthread_create的线程函数当作类的成员函数编译通过?

thread_method如果作为成员函数,必须是静态成员函数

论坛徽章:
0
10 [报告]
发表于 2003-10-09 10:04 |只看该作者

怎样把pthread_create的线程函数当作类的成员函数编译通过?

thread_method如果作为成员函数,必须是静态成员函数
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP