c - 是否可以在线程中使用退出?

标签 c linux multithreading asynchronous exit

我将 exit() 放入一个线程中,但我的程序有时不会退出。

根据这个link , exit() 不是 async-signal-safe。我想知道在线程中使用 exit() 是否会导致未定义的行为。

最佳答案

普通 exit(与 _exit 相对)需要执行所有常见的 atexit 清理、输出刷新等,工作。 有可能构造在某些情况下挂起的代码,但我不得不做一个“明显的问题”来展示它。如果库例程(例如,内部 stdio fflush)试图在其他线程持有的退出线程中获取锁(例如,在 stdio 流上),则可能会获得一个即使没有您自己的 atexit,也会出现类似的挂起。由于您没有显示您的代码,我只是推测。

这是一个测试程序(有故意的、明显的问题),它在被告知时挂起,至少在 FreeBSD 上是这样。 (格式化很棘手​​,因为剪切和粘贴保留了制表符,但后来我不得不将一些编辑成空格...)

#include <pthread.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

pthread_mutex_t global_mtx;

void die(int error, const char *fmt, ...) {
    va_list ap;

    va_start(ap, fmt);
    vfprintf(stderr, fmt, ap);
    va_end(ap);
    if (error)
        fprintf(stderr, ": %s\n", strerror(error));
    else
        putc('\n', stderr);
    fflush(stderr);
    _exit(0);
}

enum behavior { NORMAL, EXIT_WO_HANG, EXIT_W_HANG };
struct behave {
    enum behavior how;
    pthread_mutex_t lock;
    pthread_cond_t cond;
    int th1_entered;
    int th2_entered;
};

void hanger(void);

void *th1_main(void *);
void *th2_main(void *);

#define WR(x) (void)write(1, x, sizeof(x) - 1)

int main(int argc, char **argv) {
    int error;
    struct behave how;
    pthread_t th1, th2;

    error = pthread_mutex_init(&global_mtx, NULL);
    if (error)
        die(error, "pthread_mutex_init global_mtx");
    error = pthread_mutex_init(&how.lock, NULL);
    if (error)
        die(error, "pthread_mutex_init how.lock");
    error = pthread_cond_init(&how.cond, NULL);
    if (error)
        die(error, "pthread_cond_init how.cond");
    how.how = NORMAL;
    how.th1_entered = 0;
    how.th2_entered = 0;
    if (argc > 1) {
        if (strcmp(argv[1], "exit") == 0)
            how.how = EXIT_WO_HANG;
        else if (strcmp(argv[1], "hang") == 0)
            how.how = EXIT_W_HANG;
        else if (strcmp(argv[1], "normal") != 0)
            die(0, "usage: example [normal|exit|hang]");
    }
    atexit(hanger);
    error = pthread_create(&th1, NULL, th1_main, &how);
    if (error)
        die(error, "pthread_create th1");
    error = pthread_create(&th2, NULL, th2_main, &how);
    if (error)
        die(error, "pthread_create th2");
    /* now wait for threads */
    error = pthread_join(th1, NULL);
    error = pthread_join(th2, NULL);
    printf("joined, normal exit\n");
    return 0;
}

void *th1_main(void *arg) {
    struct behave *how = arg;

    WR("thread 1 start\n");
    (void) pthread_mutex_lock(&global_mtx);
    (void) pthread_mutex_lock(&how->lock);
    how->th1_entered = 1;
    pthread_cond_signal(&how->cond);
    while (how->th2_entered == 0)
        (void) pthread_cond_wait(&how->cond, &how->lock);
    WR("thread 1 sees thread 2 started\n");
    (void) pthread_mutex_unlock(&how->lock);
    if (how->how == EXIT_W_HANG)
        WR("thread 1 not unlocking\n");
    else
        (void) pthread_mutex_unlock(&global_mtx);
    return NULL;
}

void *th2_main(void *arg) {
    struct behave *how = arg;

    WR("thread 2 start\n");
    (void) pthread_mutex_lock(&how->lock);
    how->th2_entered = 1;
    pthread_cond_signal(&how->cond);
    while (how->th1_entered == 0)
        (void) pthread_cond_wait(&how->cond, &how->lock);
    WR("thread 2 sees thread 1 started\n");
    (void) pthread_mutex_unlock(&how->lock);
    if (how->how != NORMAL) {
        WR("thread 2 exit()\n");
        exit(1);
    }
    return NULL;
}

void hanger(void) {
    /* this is what will cause us to hang, in the one case */
    WR("hanger start\n");
    pthread_mutex_lock(&global_mtx);
    WR("hanger got global mutex\n");
    pthread_mutex_unlock(&global_mtx);
    WR("hanger finish\n");
}

关于c - 是否可以在线程中使用退出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16968876/

相关文章:

c - 计算阿克曼时如何检查堆栈使用情况

linux - 如何在 Ubuntu 上安装/编译 lgmask?

linux - ionice 'idle' 没有达到预期的效果

c# - C# 多线程应用程序可以为每个线程使用单独的 WorkingDirectories 吗?

c - 何时使用 waitpid() 来查找后台进程的状态

c - 正在创建文件,但未将数据推送到文件中

c - TCP 连接 : Recreating a socket that has been closed

linux - 如何让两个docker容器可以互相ping通

multithreading - 为什么 Google App Engine 只支持单线程执行?

multithreading - TCP 发送函数的奇怪行为