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的就可以了
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的就可以了
相关文章推荐
- 经典的c++下面使用pthread_create问题的解决
- Linux 中Eclipse开发C++ 11使用pthread_create()问题
- C++使用线程函数pthread_create时,调用的成员函数要定义为静态成员函数
- C++使用pthread_create()
- C++中使用pthread.h头文件报错 - 无法解析的外部符号 __imp__pthread_create,该符号在函数 _main 中被引用
- 多线程编程中使用pthread_create内存泄露问题
- 为什么在C++使用pthread_create()的时候,类成员函数做线程的处理函数必须要定义成static类型的?
- C++中使用pthread.h头文件报错 - 无法解析的外部符号 __imp__pthread_create,该符号在函数 _main 中被引用
- C++中使用pthread.h头文件报错 - 无法解析的外部符号 __imp__pthread_create,该符号在函数 _main 中被引用
- c与c++在使用pthread_create创建线程时的区别
- 为什么在C++使用pthread_create()的时候,类成员函数做线程的处理函数必须要定义成static类型的?
- c++使用thread类时编译出错,对‘pthread_create’未定义的引用
- pthread_create()使用的小问题
- c++test使用qt中的 mingw 创建pthread_create程序无法运行的问题
- c++使用thread类时编译出错,对‘pthread_create’未定义的引用
- pthread_create()和pthread_atfork()函数使用时应注意的问题
- c++使用thread类时编译出错,对‘pthread_create’未定义的引用
- linux c/c++ undefined reference to 'pthread_create' 未定义pthread_create问题
- 关于C++调用jar包,在创建虚拟机时使用JNI_CreateJavaVM调用失败返回-1的问题
- pthread_create()在C和C++使用区别