在c语言中使用pthread_create的常用方法为
这样就可以了
但是这样的代码在c++ 中却报错不能编译通过
经过向hightman请教得知在c++中函数的参数是和函数一起进行编译的,这样做的目的为了解决同名函数的重载,所以必须要吧上边的程序修改一下
这样就可以编译通过了
第二个问题也是pthread_create 的问题
在使用类成员函数作为回调函数时也会出现上边类似的情况,这个情况的解决办法网上的说法比较传统的是把这个类成员函数定义为const的就可以了
- #include
<stdio.h> - #include
<stdlib.h> - #include
<pthread.h> - void
* hello() { -
printf("hello world\n"); - }
-
- int
main() { -
pthread_t pid; -
pthread_create(&pid, NULL, hello, NULL); -
sleep(1); -
return 1; - }
- //gcc
-o test -g -Wall -lpthread test.c
这样就可以了
但是这样的代码在c++ 中却报错不能编译通过
经过向hightman请教得知在c++中函数的参数是和函数一起进行编译的,这样做的目的为了解决同名函数的重载,所以必须要吧上边的程序修改一下
- #include
<stdio.h> - #include
<stdlib.h> - #include
<pthread.h> - void
* hello(void *arg) { -
printf("hello world\n"); -
return NULL; - }
-
- int
main() { -
pthread_t pid; -
pthread_create(&pid, NULL, hello, NULL); -
sleep(1); -
return 1; - }
- //g++
-o test -g -Wall -lpthread test.c
这样就可以编译通过了
第二个问题也是pthread_create 的问题
在使用类成员函数作为回调函数时也会出现上边类似的情况,这个情况的解决办法网上的说法比较传统的是把这个类成员函数定义为const的就可以了