Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use exit in thread?

I put exit() into a thread, but my program does not exit some times.

according to this link, exit() is not async-signal-safe. I'm wondering if the use of exit() in a thread causes undefined behaviour.

like image 621
MOHAMED Avatar asked Nov 12 '22 03:11

MOHAMED


1 Answers

Ordinary exit (as opposed to _exit for instance) needs to do all the usual atexit cleanup, output-flush, etc., work. It is possible to construct code that hangs in some cases, but I had to make an "obvious problem" to show it. If a library routine (e.g., internal stdio fflush) is attempting to grab a lock (e.g., on a stdio stream) in the exiting thread that some other thread is holding, it might be possible to get a similar hang even without your own atexit. Since you have not shown your code I am merely speculating.

Here's a test program (with deliberate, obvious issue) that hangs when told to, at least on FreeBSD. (Formatting tricky because cut and paste kept tabs but then I had to edit some into spaces...)

#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");
}
like image 107
torek Avatar answered Nov 14 '22 21:11

torek