pthread_create()在C和C++使用区别
2017-06-21 11:43
330 查看
原址
pthread_create的使用
[html] view
plain copy
print?
int pthread_create(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*)
要使g++编译通过,方法如下:
C++禁止将void指针随意赋值给其他指针。
因此你在把void thread(void)函数的入口转换为void*,然后当作参数调用pthread_create时就出现错误,因为pthread_create的参数里应该是指向形如void* fun(void*)函数的一个指针。
可以修改void thread(void)为void* thread(void*),然后去掉调用时的(void*)强制转换,错误消除。
例:
[html] view
plain copy
print?
static void* thread( void* ){
int i;
for( int i=0; i<3; i++ ){
cout << " This is a thread " << endl;
}
}
int main( int arg, char** argv ){
pthread_t id;
int i, ret;
ret = pthread_create( &id, NULL, thread, NULL);
if( ret != 0 ) {
cout << " Create thread error!" << endl;
exit(1);
}
cout << "This is the main process" << endl;
pthread_join( id, NULL );
return(0);
}
换成gcc
[html] view
plain copy
print?
void thread( void ){
int i;
for( int i=0; i<3; i++ ){
cout << " This is a thread " << endl;
}
}
int main( int arg, char** argv ){
pthread_t id;
int i, ret;
ret = pthread_create( &id, NULL, (void *) thread, NULL);
if( ret != 0 ) {
cout << " Create thread error!" << endl;
exit(1);
}
cout << "This is the main process" << endl;
pthread_join( id, NULL );
return(0);
}
参考
pthread_create的使用
[html] view
plain copy
print?
int pthread_create(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*)
要使g++编译通过,方法如下:
C++禁止将void指针随意赋值给其他指针。
因此你在把void thread(void)函数的入口转换为void*,然后当作参数调用pthread_create时就出现错误,因为pthread_create的参数里应该是指向形如void* fun(void*)函数的一个指针。
可以修改void thread(void)为void* thread(void*),然后去掉调用时的(void*)强制转换,错误消除。
例:
[html] view
plain copy
print?
static void* thread( void* ){
int i;
for( int i=0; i<3; i++ ){
cout << " This is a thread " << endl;
}
}
int main( int arg, char** argv ){
pthread_t id;
int i, ret;
ret = pthread_create( &id, NULL, thread, NULL);
if( ret != 0 ) {
cout << " Create thread error!" << endl;
exit(1);
}
cout << "This is the main process" << endl;
pthread_join( id, NULL );
return(0);
}
换成gcc
[html] view
plain copy
print?
void thread( void ){
int i;
for( int i=0; i<3; i++ ){
cout << " This is a thread " << endl;
}
}
int main( int arg, char** argv ){
pthread_t id;
int i, ret;
ret = pthread_create( &id, NULL, (void *) thread, NULL);
if( ret != 0 ) {
cout << " Create thread error!" << endl;
exit(1);
}
cout << "This is the main process" << endl;
pthread_join( id, NULL );
return(0);
}
参考
相关文章推荐
- c与c++在使用pthread_create创建线程时的区别
- C++使用pthread_create()
- c++使用thread类时编译出错,对‘pthread_create’未定义的引用
- 为什么在C++使用pthread_create()的时候,类成员函数做线程的处理函数必须要定义成static类型的?
- linux下c++使用pthread_create时需要调用类成员
- c++使用thread类时编译出错,对‘pthread_create’未定义的引用
- 为什么在C++使用pthread_create()的时候,类成员函数做线程的处理函数必须要定义成static类型的?
- C++的类中使用pthread_create()
- C++中使用pthread.h头文件报错 - 无法解析的外部符号 __imp__pthread_create,该符号在函数 _main 中被引用
- C++中使用pthread.h头文件报错 - 无法解析的外部符号 __imp__pthread_create,该符号在函数 _main 中被引用
- C++中使用pthread.h头文件报错 - 无法解析的外部符号 __imp__pthread_create,该符号在函数 _main 中被引用
- C++使用线程函数pthread_create时,调用的成员函数要定义为静态成员函数
- Linux 中Eclipse开发C++ 11使用pthread_create()问题
- 经典的c++下面使用pthread_create问题的解决
- C++ 中使用pthread_create的问题
- c++使用thread类时编译出错,对‘pthread_create’未定义的引用
- Windows下使用Dev-C++开发基于pthread.h的多线程程序
- linux 下线程使用(使用pthread_create)
- 解决使用pthread_create函数造成的内存泄露
- C++使用初始化清单和直接在构造函数内初始化成员变量的区别