免费注册 查看新帖 |

Chinaunix

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

继续请教老师们,关于线程的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-02-05 22:09 |只看该作者 |倒序浏览
重新写了一个线程创建函数,但是这次能正常运行了。但是我发的第一个线程程序错在哪里?还请老师们解答
源程序
#include "stdio.h"
#include "pthread.h"
pthread_once_t once =PTHREAD_ONCE_INIT;
void run(void)
{
printf("Fuction run is running in thread %u\n",pthread_self());
}
int thread1(void)
{
pthread_t thid=pthread_self();
printf("Current thread1's ID is %u\n",thid);
pthread_once(&once,run);
printf("thread1 end\n");
return 0;
}
int thread2(void)
{
pthread_t thid=pthread_self();
printf("Current thread2's ID is %u\n",thid);
pthread_once(&once,run);
printf("thread2 end\n");
return 0;
}
int main()
{
pthread_t thid1,thid2;
pthread_create(&thid1,NULL,(void *(*)(void *))thread1,NULL);
pthread_create(&thid2,NULL,(void *(*)(void *))thread2,NULL);
sleep(3);
printf("main thread exit!,main thread ID is:%u\n",pthread_self());
exit(0);
}
下面是链接运行结果
[root@LinuxFzb tmp]# vi thid2.c
[root@LinuxFzb tmp]# gcc -o thid2 thid2.c -lpthread
[root@LinuxFzb tmp]# ./thid2
Current thread1's ID is 1082350784
Fuction run is running in thread 1082350784
thread1 end
Current thread2's ID is 1090739264
thread2 end
main thread exit!,main thread ID is:1073959552
小弟跪谢了,还请指教啊

论坛徽章:
3
戌狗
日期:2014-09-10 17:07:162015年辞旧岁徽章
日期:2015-03-03 16:54:15wusuopu
日期:2016-06-17 17:43:45
2 [报告]
发表于 2009-02-06 02:11 |只看该作者

回复 #1 nervfzb 的帖子

那个帖子中pthread_create的参数不对。
if(pthread_create(&thid,NULL,(void*(*)(void *))thread(),NULL)!=0)
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP