您的位置:首页 > 编程语言 > C语言/C++

C++ 中使用pthread_create的问题

2013-01-12 18:07 459 查看
在c语言中使用pthread_create的常用方法为

view plainprint?

#include
<stdio.h>

#include
<stdlib.h>

#include
<pthread.h>

void*
hello() {

printf("helloworld\n");

}

intmain()
{

pthread_t pid;

pthread_create(&pid, NULL,
hello, NULL);

sleep(1);

return1;

}

//gcc -o test
-g -Wall -lpthread test.c

这样就可以了

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

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

view plainprint?

#include
<stdio.h>

#include
<stdlib.h>

#include
<pthread.h>

void*
hello(void*arg)
{

printf("helloworld\n");

returnNULL;

}

intmain()
{

pthread_t pid;

pthread_create(&pid, NULL,
hello, NULL);

sleep(1);

return1;

}

//g++ -o test
-g -Wall -lpthread test.c

这样就可以编译通过了

第二个问题也是pthread_create 的问题

在使用类成员函数作为回调函数时也会出现上边类似的情况,这个情况的解决办法网上的说法比较传统的是把这个类成员函数定义为const的就可以了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐