LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 953|回复: 4

pthread_cancel的问题

[复制链接]
发表于 2004-9-8 19:22:30 | 显示全部楼层 |阅读模式
小弟在调试程序时发现一个问题:

如果编译时用了-O参数的话.在一些调用pthread_cancel的情况下会出现"Segmentation fault".

不加-O参数则没有遇到过.

下面是简化后的代码,大虾帮忙看一下是不是的代码的问题.

#include <pthread.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sched.h>
#include <fcntl.h>
#include <unistd.h>
#include <semaphore.h>
#include <stdio.h>
#include <errno.h>

                                                                                                                           
int usec_sleep(unsigned long usec) {
        struct timeval time_out;
        time_out.tv_sec = usec / 1000000;
        time_out.tv_usec = usec % 1000000;
        while (select(0, NULL, NULL, NULL, &time_out) == -1) {
                if (errno != EINTR)
                        return -1;
        }
        return 0;
}

static sem_t target_lock;

static void* do_something(void *data) {
        int i;
        while (1) {
                sem_wait(&target_lock);
                // do something
                      sem_post(&target_lock);
                for (i = 0; i < 64; ++i)
                        //do something
                        usec_sleep(8000);
        }
        pthread_exit(NULL);
}

int main(int argc, char *argv[]) {
        int in, fd, lock_free;
        pthread_t thread;
       
        lock_free = 0;
        sem_init (&target_lock, 0, 0);
        pthread_create(&thread, NULL, do_something, NULL);
       
        while (1) {
                scanf("%d", &in);
                if (in == -1) {
                        break;
                }
                if (in == 0) {
                        if (lock_free)
                                sem_wait(&target_lock);
                        lock_free = 0;
                }
                if (in == 1) {
                        if (lock_free) {
                                sem_wait(&target_lock);
                                // do something
                                sem_post(&target_lock);
                        }
                        else {
                                // do something
                                sem_post(&target_lock);
                        }
                        lock_free = 1;
                }
        }
        pthread_cancel(thread);
        pthread_join(thread, NULL);
}


当用 gcc -O test.c -lpthread编译后:
[daemeon@localhost tmp]$ ./a.out
1
0
-1
Segmentation fault
 楼主| 发表于 2004-9-8 19:31:47 | 显示全部楼层
#include <pthread.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sched.h>
#include <fcntl.h>
#include <unistd.h>
#include <semaphore.h>
#include <stdio.h>
#include <errno.h>

                                                                                                                           
int usec_sleep(unsigned long usec) {
        struct timeval time_out;
        time_out.tv_sec = usec / 1000000;
        time_out.tv_usec = usec % 1000000;
        while (select(0, NULL, NULL, NULL, &time_out) == -1) {
                if (errno != EINTR)
                        return -1;
        }
        return 0;
}

static sem_t target_lock;

static void* do_something(void *data) {
        int i;
        while (1) {
                sem_wait(&target_lock);
                // do something
                sem_post(&target_lock);
                for (i = 0; i < 64; ++i)
                        //do something
                        usec_sleep(8000);
        }
        pthread_exit(NULL);
}

int main(int argc, char *argv[]) {
        int in, fd, lock_free;
        pthread_t thread;
       
        lock_free = 0;
        sem_init (&target_lock, 0, 0);
        pthread_create(&thread, NULL, do_something, NULL);
       
        while (1) {
                scanf("%d", &in);
                if (in == -1) {
                        break;
                }
                if (in == 0) {
                        if (lock_free)
                                sem_wait(&target_lock);
                        lock_free = 0;
                }
                if (in == 1) {
                        if (lock_free) {
                                sem_wait(&target_lock);
                                // do something
                                sem_post(&target_lock);
                        }
                        else {
                                // do something
                                sem_post(&target_lock);
                        }
                        lock_free = 1;
                }
        }
        pthread_cancel(thread);
        pthread_join(thread, NULL);
}
发表于 2004-9-8 19:53:49 | 显示全部楼层
-O的优化打开了-fthread-jumps,看看是不是这个原因吧.
 楼主| 发表于 2004-9-8 23:18:22 | 显示全部楼层
好象不是
直接加-fthread-jumps编译
运行时没有出现Segmentation fault

请问这个问题是不是代码的问题?
发表于 2004-9-10 23:04:04 | 显示全部楼层
-D_REENTRANT
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表