免费注册 查看新帖 |

Chinaunix

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

杀死一个线程的时候出现段错误,请教!! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-09-26 19:24 |只看该作者 |倒序浏览
下面的程序,当线程执行了一次超时处理之后杀死它会出现段错误,弄不明白,
是什么


#include <stdio.h>
#include <sys/time.h>
#include <pthread.h>

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
void *unlock_mutex(void *arg)
{
        //pthread_mutex_t *temp = (pthread_mutex_t *)arg;
        pthread_mutex_unlock(&mutex);
}
void *thread_func(void *temp)
{
        //pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
        //pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,NULL);
        struct timespec abstime;
        memset(&abstime, 0, sizeof(struct timespec));
        abstime.tv_sec = time(NULL) + 4;
        abstime.tv_nsec = 0;
        pthread_mutex_lock(&mutex);
        //pthread_cleanup_push(pthread_mutex_unlock, &mutex);
        while(1)
        {
                //pthread_mutex_lock(&mutex);
                //int ret = pthread_cond_wait(&cond, &mutex);
                int ret = pthread_cond_timedwait(&cond, &mutex, &abstime);
                if(ret!=0)
                {
                        printf("time out in thread\n");
                        abstime.tv_sec = time(NULL) + 4;
                        // pthread_mutex_unlock(&mutex);
                        continue;
                }
                printf("cond successful\n");
        }
        //pthread_cleanup_pop(0);
        pthread_mutex_unlock(&mutex);
}

int main()
{
        pthread_t id;
        pthread_create(&id, NULL,&thread_func, NULL);
        //sleep(2);
        //pthread_cond_signal(&cond);
        sleep(6);
        printf("return :%d\n",pthread_cancel(id));
        pthread_join(id, NULL);
        //printf("hello!\n");
        //while(1){};
        return 0;
}


gdb调试的时候提示段出错的位置在
#0  0x00000000 in ?? ()
#1  0x4003cd19 in __pthread_cleanup_upto () from /lib/tls/libpthread.so.0
#2  0x42027301 in _longjmp_unwind () from /lib/tls/libc.so.6
#3  0x4202726c in siglongjmp () from /lib/tls/libc.so.6
#4  0x40038ea4 in sigcancel_handler () from /lib/tls/libpthread.so.0
#5  <signal handler called>
#6  0xffffe002 in ?? ()


系统是REDHAT9

论坛徽章:
0
2 [报告]
发表于 2006-09-26 19:26 |只看该作者
无论加不加资源处理函数都是一样的结果,主要是在join的时候出现错误

论坛徽章:
0
3 [报告]
发表于 2006-09-27 14:40 |只看该作者
我这里运行正常,linux上.

论坛徽章:
0
4 [报告]
发表于 2006-09-28 15:23 |只看该作者
我这里也是正常的
RHEL4
内核2.6.9-22

论坛徽章:
0
5 [报告]
发表于 2006-09-29 08:15 |只看该作者
你编译的时候是否加上了参数 -lpthread

论坛徽章:
0
6 [报告]
发表于 2006-09-30 10:25 |只看该作者
已经找的原因了,是红帽子9和POSIX线程库的问题,我在FC5上跑正常,只在redhat9上会出这样的问题.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP