pthread_join 函数报段错误 ??????
#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.自己查看了下说明文档,就是这样用啊,为啥会报错呢?求各位前辈指点。

[解决办法]
我运行了一下 一切正常
start
thread_fn is running
thread will return
will join
pthread_join error: Success
环境 gcc 版本 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)
[解决办法]
进程意外退出会在当前目录下产生形如‘core.数字’的文件比如‘core.1234’
使用命令
gdb 运行程序名 core.数字
进入gdb然后使用bt命令
可以查看进程意外退出前函数调用的堆栈,内容为从上到下列出对应从里层到外层的函数调用历史。
如果进程意外退出不产生core文件,参考“ulimit -c core文件最大块大小”命令
[解决办法]
找到原因就好