- 论坛徽章:
- 0
|
下面的程序,当线程执行了一次超时处理之后杀死它会出现段错误,弄不明白,
是什么
#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 |
|