免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2082 | 回复: 5
打印 上一主题 下一主题

在请教老师们一个问题,教教我吧 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-02-05 18:19 |只看该作者 |倒序浏览
在学习使用pthread_create函数创建线程时候,为了查看是否创建成功,写了这样一个程序
#include "stdio.h"
#include "stdlib.h"
#include "unistd.h"
#include "pthread.h"
int thread(void)
{
pthread_t newthid;
newthid=pthread_self();
printf("this is a new thread,thread ID =%u\n",newthid);
return 0;
}
int main(void)
{
pthread_t thid;
printf("main thread,ID is %u\n",pthread_self());
if(pthread_create(&thid,NULL,(void*(*)(void *))thread(),NULL)!=0)
{
  printf("thread creation failed\n");
  exit(1);
}
sleep(1);
exit(0);
}
在编译的时候也加了链接库了,编译是没有问题的,但是我运行的时候
程序没有提示未创建起新线程,但是我在线程函数里得到的线程ID和主函数得到的线程ID值是相同的,而且在末尾系统自动提示"段错误“请问这是啥问题?

论坛徽章:
0
2 [报告]
发表于 2009-02-05 19:05 |只看该作者
把我用gcc编译连接的图发上来,请老师们看看啊

问题.JPG (12.66 KB, 下载次数: 34)

问题.JPG

论坛徽章:
3
戌狗
日期:2014-09-10 17:07:162015年辞旧岁徽章
日期:2015-03-03 16:54:15wusuopu
日期:2016-06-17 17:43:45
3 [报告]
发表于 2009-02-06 02:10 |只看该作者
原帖由 nervfzb 于 2009-2-5 18:19 发表
if(pthread_create(&thid,NULL,(void*(*)(void *))thread(),NULL)!=0)

去掉红色的括号。thread() -> thread

论坛徽章:
0
4 [报告]
发表于 2009-02-07 10:07 |只看该作者
ret = pthread_create(&thread_id,NULL,(void*)thread_spcaview,NULL);

给你一个别人的例子看看。

论坛徽章:
3
戌狗
日期:2014-09-10 17:07:162015年辞旧岁徽章
日期:2015-03-03 16:54:15wusuopu
日期:2016-06-17 17:43:45
5 [报告]
发表于 2009-02-08 00:07 |只看该作者
原帖由 huyongzs 于 2009-2-7 10:07 发表
ret = pthread_create(&thread_id,NULL,(void*)thread_spcaview,NULL);

给你一个别人的例子看看。

楼主线程函数的类型够复杂 (void*(*)(void *))

论坛徽章:
0
6 [报告]
发表于 2009-02-08 12:01 |只看该作者
(int(*)(void))  ?


http://jqbsx.blog.sohu.com/

[ 本帖最后由 jqbsx 于 2009-2-8 12:08 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP