免费注册 查看新帖 |

Chinaunix

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

[C++] 线程函数pthread_create使用类的非静态成员函数问题! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-02-17 17:49 |只看该作者 |倒序浏览
本帖最后由 zhanhorse 于 2014-02-18 12:55 编辑

线程函数pthread_create使用类的非静态成员函数,但编译的时候报错。
请大侠指点一下是模板类定义错误,还是调用模板类的时候有问题。谢谢!

定义模板类:
template <typename TYPE, void (TYPE::ReadCustTypePointFile)>
void* _thread_t(void* param)
{
    TYPE* This = (TYPE*)param;
    This->ReadCustTypePointFile();
        return NULL;
}

class CustPoint
{
public:
        void *ReadCustTypePointFile(void *SourFile);

}

CustPoint::CustPoint()
{
        //调用线程函数
  pthread_create(pthread_serv,NULL,_thread_t<CustPoint, CustPoint::ReadCustTypePointFile>,(void *)(&SourFile));
}

"CustPoint.cpp", line 453: error #2386: no instance of function template
          "_thread_t" matches the required type
                pthread_create(pthread_serv,NULL,_thread_t<CustPoint, CustPoint::ReadCustTypePointFile>,(void *)(&SourFile));

论坛徽章:
15
射手座
日期:2014-11-29 19:22:4915-16赛季CBA联赛之青岛
日期:2017-11-17 13:20:09黑曼巴
日期:2017-07-13 19:13:4715-16赛季CBA联赛之四川
日期:2017-02-07 21:08:572015年亚冠纪念徽章
日期:2015-11-06 12:31:58每日论坛发贴之星
日期:2015-08-04 06:20:00程序设计版块每日发帖之星
日期:2015-08-04 06:20:00程序设计版块每日发帖之星
日期:2015-07-12 22:20:002015亚冠之浦和红钻
日期:2015-07-08 10:10:132015亚冠之大阪钢巴
日期:2015-06-29 11:21:122015亚冠之广州恒大
日期:2015-05-22 21:55:412015年亚洲杯之伊朗
日期:2015-04-10 16:28:25
2 [报告]
发表于 2014-02-25 15:43 |只看该作者
本帖最后由 yulihua49 于 2014-02-25 15:44 编辑
zhanhorse 发表于 2014-02-17 17:49
线程函数pthread_create使用类的非静态成员函数,但编译的时候报错。
请大侠指点一下是模板类定义错误,还 ...

pthread_create如何提供this?
必须是:
extern "C" void * func(void *);

论坛徽章:
0
3 [报告]
发表于 2014-04-25 10:23 |只看该作者
回调应该只能是静态函数, 可以用下面的方法:
1, 用std::function和std::bind, 可以将非静态成员函数作为回调函数.
2, 另外写一个callback proxy, 将要调用的成员函数的对象赋值给一个变量,然后通过这个变量来调用.


您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP