免费注册 查看新帖 |

Chinaunix

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

关于pthread编译的一个奇怪问题! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-09-16 11:25 |只看该作者 |倒序浏览
我自己写了一个很简单的pthread程序,前面用了#include <pthread.h>
然后用gcc myPthread.c居然能通过编译????
但是运行时,pthread_creat函数却没有正确地创建线程。但为什么能通过编译呢?

我知道正确的编译应该是:gcc myPthread.c -lpthread

但是,奇怪的是,为什么用gcc myPthread.c也能通过编译?
谢谢大侠的回答!

论坛徽章:
0
2 [报告]
发表于 2006-09-16 12:57 |只看该作者
程序帖出来

论坛徽章:
0
3 [报告]
发表于 2006-09-16 14:29 |只看该作者
程序基本就是高程里面的内容了,如下:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <pthread.h>


pthread_t pthid;

void printids(const char *s)
{
  pid_t      pid;
  pthread_t  tid;
  
  pid=getpid();
  tid=pthread_self();
  //tid=pthid;
  printf("%s pid %u tid %u (0x%x)\n", s,pid, tid, tid);
}

void *thr_fn(void *arg)
{
  printids("new  thread: ");
  //return(0);
  pthread_exit((void *)2);
}

int main(void)
{
int err;
void *t_ret;
err=pthread_create(&pthid,NULL,thr_fn,NULL);
if(err!=0) printf("creat thread unsuccessful!\n");
printids("main thread: ");
err=pthread_join(pthid,&t_ret);
if(err!=0) printf("cannot join with phtid!\n");
printf("thread exit code: %d \n",t_ret);
//sleep(1);
exit(0);
}


运行环境:Solaris 9

编译和运行1:
# gcc myPthread.c
# a.out
creat thread unsuccessful!
main thread:  pid 23984 tid 1 (0x1)
thread exit code: 4
#

编译和运行2:
# gcc myPthread.c -lpthread
# a.out
main thread:  pid 23990 tid 1 (0x1)
new  thread:  pid 23990 tid 2 (0x2)
thread exit code: 2
#

论坛徽章:
0
4 [报告]
发表于 2006-09-16 14:32 |只看该作者
显然,上述的“编译和代码1”是错误的,但奇怪的是,居然能通过编译。
大家讨论讨论呢。
谢谢!

论坛徽章:
0
5 [报告]
发表于 2006-09-17 11:06 |只看该作者
我的问题没人回答??????
求救斑竹了

论坛徽章:
0
6 [报告]
发表于 2006-09-17 16:12 |只看该作者
很显然,你的机器有问题.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP