C++ 中使用pthread_create的问题

在c语言中使用pthread_create的常用方法为

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <pthread.h>
  4. void* hello() {
  5. printf("helloworld\n");
  6. }
  7. intmain() {
  8. pthread_t pid;
  9. pthread_create(&pid, NULL, hello, NULL);
  10. sleep(1);
  11. return1;
  12. }
  13. //gcc -o test -g -Wall -lpthread test.c


这样就可以了

但是这样的代码在c++ 中却报错不能编译通过

经过向hightman请教得知在c++中函数的参数是和函数一起进行编译的,这样做的目的为了解决同名函数的重载,所以必须要吧上边的程序修改一下

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <pthread.h>
  4. void* hello(void*arg) {
  5. printf("helloworld\n");
  6. returnNULL;
  7. }
  8. intmain() {
  9. pthread_t pid;
  10. pthread_create(&pid, NULL, hello, NULL);
  11. sleep(1);
  12. return1;
  13. }
  14. //g++ -o test -g -Wall -lpthread test.c


这样就可以编译通过了

第二个问题也是pthread_create 的问题

在使用类成员函数作为回调函数时也会出现上边类似的情况,这个情况的解决办法网上的说法比较传统的是把这个类成员函数定义为const的就可以了
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值