多线程-linux
时间:2010-08-23
来源:互联网
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
-
- void thread( void )
- {
- int i ;
- for ( i = 0; i < 3; ++i )
- {
- printf("this is a pthread\n");
- }
- }
-
-
- int main()
- {
- pthread_t id;
- int i , ret;
-
- ret = pthread_create( &id, NULL, (void *)thread, NULL );
- if ( 0 != ret )
- {
- printf("Create pthread error!\n");
- exit(1);
- }
- for ( i = 0; i < 3; ++i )
- {
- printf("this is the main process. \n");
- pthread_join( id, NULL );
- }
- /*pthread_timedjoin_np();*/
-
- return 0;
- }
gcc test.c -lpthread
编译通过,运行也ok
但是使用
g++ test.c -lpthread
却报错为
example1.c: In function ‘int main()’:
example1.c:20: error: invalid conversion from ‘void*’ to ‘void* (*)(void*)’
example1.c:20: error: initializing argument 3 of ‘int pthread_create(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*)’
请问原因为何?
还请问下有什么多线程编程的好资料推荐
谢谢
作者: alexandnpu 发布时间: 2010-08-23
- void thread( void )
- void* thread( void * p )
作者: jackin0627 发布时间: 2010-08-23
作者: tajial 发布时间: 2010-08-23
作者: jnjn999 发布时间: 2010-08-23
还是不行啊,g++还是报出相同的错
我的g++是4.4.4 Fedora 13 的系统
作者: alexandnpu 发布时间: 2010-08-23
- void thread( void )
- void* thread( void* p )
- ret = pthread_create( &id, NULL, (void *)thread, NULL );
- ret = pthread_create( &id, NULL, thread, NULL );
作者: jackin0627 发布时间: 2010-08-23
我把
- #
-
-
- # ret = pthread_create( &id, NULL, (void *)thread, NULL );
- #
-
-
- # ret = pthread_create( &id, NULL, thread, NULL );
那thread函数本身就是返回void *的,我多此一举的在pthread_create函数中轻质转换(void *),为什么会报错呢?
给个解释吧
作者: alexandnpu 发布时间: 2010-08-23
- ret = pthread_create( &id, NULL, (void *)thread, NULL );
- ret = pthread_create( &id, NULL, (void * (*)(void*))thread, NULL );
作者: jackin0627 发布时间: 2010-08-23
我把改为之后就ok了,
那thread函数本身就是返回void *的,我多此一举的在pth ...
alexandnpu 发表于 2010-08-23 16:26
前面的错误代码解释的很清楚了
invalid conversion from ‘void*’ to ‘void* (*)(void*)’
这两个类型不兼容。。
建议搜索下 C语言函数指针的相关内容
作者: davelv 发布时间: 2010-08-23
嗯,刚看明白了,原来的那个thread本身是个函数指针,已经符合参数要求了,我加了一个(void *),以为是匹配了参数,实际上是把一个函数指针转换成了void* 指针,所以编译器会报出那样的错误
谢谢各位指点
作者: alexandnpu 发布时间: 2010-08-23
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28