pthread_join( .. ) 리턴값에 대하여.

deisys의 이미지

여기저기 뒤져보고 맨페이지도 보니까
성공한 경우에 0을 리턴한다고 하는데요...
이 말은 인자로 넘겨준 thread 가 종료했을때, 0을 리턴한다는게 아닌가요?

#define _REENTRANT
#include <stdio.h>
#include <pthread.h>

void * start_function(void * arg);

int main()
{
 int i;
 pthread_t thread;
 void * ret;

 thread=pthread_create(&thread, NULL, start_function, NULL);
 for(i=0;i<10000;i++)
   printf("Parent ! %d\n", i);

 do
 {
   i=pthread_join(thread, &ret);
   printf("i=%d\n", i);
 } while(i!=0);

 pthread_exit(0);
}

void * start_function(void * arg)
{
 int i;

 for(i=0;i<10000;i++)
   printf("Thread ! %d\n", i);

 pthread_exit(0);
}

이렇게 하면 Parent! Thread! 를 다 찍은 후에도 i=3 만 계속 찍으면서 무한루프를 도는군요. ... pthread_join 을 어떻게 써야 하는건지 좀 알려주세요 :-D

june8th의 이미지

deisys wrote:
thread=pthread_create(&thread, NULL, start_function, NULL);

이 부분이 틀렸습니다.
pthread_create의 return 값으로 thread를 덮어쓰기때문에
pthread_join의 ret값이 3인(ESRCH인) 오류가 나네요..

verena의 이미지

3번은 ESRCH 즉 해당 쓰레드가 존재하지 않는다란 뜻입니다.

위의 코드를 보면 pthread_join을 하기 이전에 thread가 종료할 가능성이 충분합니다.

그러니 무조건 정상적인 0의 값에 대해서만 처리해서는 안됩니다.

------
아 윗분글이 먼저 달렸네요... 네 윗분 말씀이 맞습니다.

deisys의 이미지

감사합니다. :-D