pthread_join 函数报段错误 ??????

微信公众号 2013-03-28 04:16:35
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

void *thread_fn(void*);
struct foo* foo_aloc(void);
void foo_hold(struct foo*);
void foo_rele(struct foo*);
void my_err(char *);

struct foo
{
int f_count;
pthread_mutex_t f_lock;
/* More stuff here */
};

//主函数
int main(void)
{
printf("start\n");

struct foo*fp;
pthread_t tid;
/* 为了便于调试错误,注掉了
if((fp=foo_aloc())==NULL)
my_err("foo_aloc error\n");
*/
if((tid=pthread_create(&tid,NULL,thread_fn,NULL))!=0)
my_err("pthread_create error\n");
sleep(1);
printf("will join\n");
/* Wait the second thread run over */
//错误就在这里:join函数报段错误!谁能告诉我,这是为什么?????
if(pthread_join(tid,NULL)!=0)
my_err("pthread_join error");
// printf("after join,pthread_exit return:%d\n",(int)ret);
int i;
printf("after join\n");
/*
for(i=0;i<10;i++)
{
printf("f_count:%d\n",fp->f_count);
foo_rele(fp);
}
if(fp==NULL)
printf("fp is NULL now");
*/
printf("end\n");
exit(0);
}

void* thread_fn(void *arg)
{
printf("thread_fn is running\n");
/* int i;
struct foo *fp=(struct foo*)arg;
for(i=0;i<10;i++)
{
fp->f_count++;
}
*/
printf("thread will return \n");
return (void*)0;
}

struct foo* foo_aloc(void)
{
struct foo *fp;
if((fp=malloc(sizeof(struct foo)))!=NULL)
{
//fp->f_count=1;
if(pthread_mutex_init(&fp->f_lock,NULL)!=0)
{
free(fp);
return NULL;
}
/* Continue initialization */
}
return fp;
}

/* Add a reference to the object */
void foo_hold(struct foo *fp)
{
pthread_mutex_lock(&fp->f_lock);
fp->f_count++;
pthread_mutex_unlock(&fp->f_lock);
}

/* Release a reference to the object */
void foo_rele(struct foo *fp)
{
pthread_mutex_lock(&fp->f_lock);
if(--fp->f_count==0)
{
pthread_mutex_unlock(&fp->f_lock);
pthread_mutex_destroy(&fp->f_lock);
free(fp);
}
else {
pthread_mutex_unlock(&fp->f_lock);
}
}

void my_err(char *str)
{
perror(str);
exit(1);
}


主函数中,为便于调试,注掉了一些代码。当运行到pthread_join 时,程序报段错误:Segmentation fault.自己查看了下说明文档,就是这样用啊,为啥会报错呢?求各位前辈指点。


...全文
1031 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
drew926 2013-07-25
  • 打赏
  • 举报
回复
犯了同样的错误啊!
wshn13 2013-03-28
  • 打赏
  • 举报
回复
引用 6 楼 jiajiayouba 的回复:
引用 4 楼 wshn13 的回复:引用 3 楼 jiajiayouba 的回复:引用 2 楼 wshn13 的回复:我运行了一下 一切正常 start thread_fn is running thread will return will join pthread_join error: Success 环境 gcc 版本 4.6.3 (Ubuntu……
找到原因就好
微信公众号 2013-03-28
  • 打赏
  • 举报
回复
引用 4 楼 wshn13 的回复:
引用 3 楼 jiajiayouba 的回复:引用 2 楼 wshn13 的回复:我运行了一下 一切正常 start thread_fn is running thread will return will join pthread_join error: Success 环境 gcc 版本 4.6.3 (Ubuntu/Linaro 4.6.3-1ubu……
唉,是自己太马虎了。一位前辈给找到错误了。是if((tid=pthread_create(&tid,NULL,thread_fn,NULL))!=0) 这里出错了,pthread_create 成功返回0,失败返回错误码。我这里把线程的id用0给覆盖了……泪奔啊……
赵4老师 2013-03-28
  • 打赏
  • 举报
回复
进程意外退出会在当前目录下产生形如‘core.数字’的文件比如‘core.1234’ 使用命令 gdb 运行程序名 core.数字 进入gdb然后使用bt命令 可以查看进程意外退出前函数调用的堆栈,内容为从上到下列出对应从里层到外层的函数调用历史。 如果进程意外退出不产生core文件,参考“ulimit -c core文件最大块大小”命令
wshn13 2013-03-28
  • 打赏
  • 举报
回复
引用 3 楼 jiajiayouba 的回复:
引用 2 楼 wshn13 的回复:我运行了一下 一切正常 start thread_fn is running thread will return will join pthread_join error: Success 环境 gcc 版本 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)我用的是gcc 4.4.4 会是gc……
不会是吧
微信公众号 2013-03-28
  • 打赏
  • 举报
回复
引用 2 楼 wshn13 的回复:
我运行了一下 一切正常 start thread_fn is running thread will return will join pthread_join error: Success 环境 gcc 版本 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)
我用的是gcc 4.4.4 会是gcc的问题吗?……
wshn13 2013-03-28
  • 打赏
  • 举报
回复
我运行了一下 一切正常 start thread_fn is running thread will return will join pthread_join error: Success 环境 gcc 版本 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)
微信公众号 2013-03-28
  • 打赏
  • 举报
回复
各位帮帮忙,自己顶一下。

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧