이상한 문제. 나는 내 스레드를 여기에 산다. 이걸 죽일 때까지 계속 반복해야합니다.스레드가 완료되지 않음 (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;
}
왜 마지막 문이 인쇄되지 않습니다? 스레드가 돌아올 때까지 기다려야하는 pthread_join을 호출하고 종료합니다.
내 스레드가 올바르게 끝났습니까?
stdout
이 라인 버퍼링하고 마지막printf()
에서 줄 바꿈 (\n
)를 가지고 있지 않기 때문에
'thread'가 종료되고 'return'값이 없어야합니다. 'return NULL'을'pthread_exit()'로 변경하면 코드가 잘 동작합니다. – Ganesh
@Ganesh 콜링 리턴은 완벽하게 유효합니다. 'return foo;'는'pthread_exit (foo);에 해당합니다. _unless_ 문제의 스레드는 주 스레드입니다. –