2013-11-27 3 views
0

이상한 문제. 나는 내 스레드를 여기에 산다. 이걸 죽일 때까지 계속 반복해야합니다.스레드가 완료되지 않음 (pthread_join을 호출하고 있음에도 불구하고)

void accept_connections(int sock_fd) 
{ 
    while(1) 
    { 
      /*Whole bunch of irrelevant stuff*/ 
     pthread_create(&(new_connect->thread), NULL, &thread_dispatch, new_connect); 
     printf("Thread spawned.\n"); 
     pthread_join(new_connect->thread, NULL); 
      /*Exit when catches a sigint */ 
    } 
} 

그리고 함수의 pthreads 실행 :

void* thread_dispatch(void* new_connect) 
{ 

    printf("Thread working.\n"); 

    http_t *http = malloc(sizeof(http_t)); 

    int bytes_read = http_read(http, fd); 

    printf("read %d\n",bytes_read); //this prints 
    printf("status %s\n",http->status); //this prints 
    printf("body %s\n",http->body); //this prints 
    const char* get_status = http_get_status(http); 
    char* filename = process_http_header_request(get_status); 
    printf("filename: %s", filename); //this doesn't print unless I do exit(1) on next line 
    return NULL; 
} 
  1. 왜 마지막 문이 인쇄되지 않습니다? 스레드가 돌아올 때까지 기다려야하는 pthread_join을 호출하고 종료합니다.

  2. 내 스레드가 올바르게 끝났습니까? stdout이 라인 버퍼링하고 마지막 printf()에서 줄 바꿈 (\n)를 가지고 있지 않기 때문에

+0

'thread'가 종료되고 'return'값이 없어야합니다. 'return NULL'을'pthread_exit()'로 변경하면 코드가 잘 동작합니다. – Ganesh

+2

@Ganesh 콜링 리턴은 완벽하게 유효합니다. 'return foo;'는'pthread_exit (foo);에 해당합니다. _unless_ 문제의 스레드는 주 스레드입니다. –

답변

3

귀하의 마지막 행은 인쇄되지 않습니다. exit()stdout 버퍼를 비울 가능성이 있습니다.

+2

필자는 아마도 printf()에서 줄 바꿈을 원한다고 생각하지만, printf()와 return 사이에'fflush (stdout); 버퍼 관례에 따라 개행 문자가있을 때마다 stdout이 비워 지지만 필요한 표준이 있는지 확신 할 수 없습니다. 다른 말로하면, 개행이있을 때 버퍼를 플러시하는 것은 OS _not_에 대해 합법적이라고 생각합니다. 사람들이 가장 많이 사용하는 것들이 발생합니다. –

관련 문제