开发者社区> 问答> 正文

C++使用pthread_create创建线程报错 ?报错

我使用的操作系统是ubuntu 12.04
报错的代码段如下:

pthread_t id;
 int ret = pthread_create(&id,NULL,(void *)func,NULL);

然后报错内容是:invalid use of member(did you forget the '&'?)
这句话我知道啥意思,但是不知道哪里少了取地址符,求大神赐教

展开
收起
爱吃鱼的程序员 2020-06-23 20:51:51 638 0
1 条回答
写回答
取消 提交回答
  • /profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    2000元阿里云代金券免费领取,2核4G云服务器仅664元/3年,新老用户都有优惠,立即抢购>>>

    搞定了,是线程函数要使用静态类型

    还有编译器是gcc4.4.7

    void*msg_thread(void*ptr);
    pthread_tpid;
    pthread_create(&pid,NULL,msg_thread,null);

    void*msg_thread(void*ptr){
    while(1){
    printf("ok");
    }
    }

    直接把(void*)改成&试试,如果线程函数定义为函数指针,pthread_create里边参数应该直接就是函数指针的名字,如果是一般函数,怎要加&取地址

    试试

    pthread_create(&id,NULL,(void*)&func,NULL)

    操作系统和编译器都和楼主不一样(Ubuntu16.04,gcc5.3.1),仅供参考,感觉楼主的代码在我机器上应该能正常编译。。下面两段代码在我机器上都是可以跑的。。

    #include<pthread.h>#include<stdio.h>voidmy_method(void*whatever){printf("Called\n");}intmain(intargc,char**argv){pthread_tthread;pthread_create(&thread,NULL,(void*)&my_method,NULL);pthread_join(thread,NULL);return0;}
    #include<pthread.h>#include<stdio.h>voidmy_method(void*whatever){printf("Called\n");}intmain(intargc,char**argv){pthread_tthread;pthread_create(&thread,NULL,(void*)my_method,NULL);pthread_join(thread,NULL);return0;}
    2020-06-23 20:52:09
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
使用C++11开发PHP7扩展 立即下载
GPON Class C++ SFP O;T Transce 立即下载
GPON Class C++ SFP OLT Transce 立即下载
http://www.vxiaotou.com