2013-06-06 3 views
4

exit()을 스레드에 넣었지만 프로그램이 종료되지 않습니다.스레드에서 exit를 사용할 수 있습니까?

에 의하면, link, exit()async-signal-safe이 아니다. 스레드에서 exit()을 사용하면 정의되지 않은 동작이 발생하는지 궁금합니다.

+2

'exit_group()'또는 '_exit()'을 확인하십시오. – FatalError

+0

'exit()'는 단일 스레드 프로세스에서만 의미가 있다고 생각합니다. –

+0

디버거에서 재현 할 수 있습니까? 프로그램이 무엇을하는지 알아야합니다. 교착 상태이거나 라이브 잠금 또는 일부 무한 루프가 손상된 데이터 구조 (또는 일부 동등하게 나쁜 상태)를 걷고 있기 때문에 발생할 수 있습니다. – jxh

답변

1

보통 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"); 
} 
+1

관련 질문을 여기에서 요청했습니다. http://stackoverflow.com/questions/12549704/glibc-possible-race-condition-between-closing-file- while-exiting, 경쟁 조건의 일부 변형은 쉽게 교착 상태로 이어지고 프로세스도 중단됩니다. (exit()은 main에서 리턴하는 것과 비슷하다.) – nos

+0

내 POSIX 스레딩이 있기 훨씬 전에 작성된 내 자신의 stdio 코드에서, 필자의 exit-cleanup 코드는 모든 파일을'fflush ' 그들을 fclose. 이것은 모든 문제를 막지는 못하며 "스레드는 닫힌 stdio 스트림을 사용합니다"가 발생하지 않게합니다. (실제로 * 계획을 세웠다 * 내가 그것을 썼을 때, 효율성을 위해서였다 :'_exit'는 모든 메모리를 해제하고 모든 파일을 닫았지만, 왜 여분의 시스템 호출을해야 하는가?) – torek

+0

'_fini 일부 뮤텍스가 아직 해제되지 않았거나'_fini() '함수가 작동중인 전역 상태가 일관된 상태가 아니기 때문에 공유 라이브러리의() – jxh

관련 문제