[완료] thread 함수 안에서의 pthread_join() 호출 관련 질문입니다.

hahaite의 이미지

안녕하세요.

pthread_join() 사용과 관련하여 질문을 드립니다.

아래 코드 간단히 설명드리면,
종료하라는 신호가 오면 내 thread_id 값을 추출하고,
함수 끝에서 pthread_join() 을 하는 구조인데요.

void* threadTest(void* pData)
{
        pthread_t threadSelf ;
        int status ;
 
        while(1)
        {
                if(m_flagDestroy)
                {
                        threadSelf = pthread_self() ;
                        break ;
                }
                usleep(1) ;
        }
 
        pthread_join(threadSelf, (void**)&status) ;
        printf("status : %d\n", status) ;
        return NULL ;
}

궁금한게, pthread_join() 은 쓰레드가 종료할 때까지 대기하는 함수로 알고 있는데,
위와 같은 구조이면 자기가 자기를 감시(?) 하는 구조가 되는거잖아요.

돌려보면 잘 돌고...

위 코드가 유효한걸까요?

고수님들의 답변 부탁드립니다.

그럼, 즐거운 하루 되세요.

pastime의 이미지

리턴값을 체크해 보셔야 합니다.

ERRORS
       ...
 
       The pthread_join() function may fail if:
 
       EDEADLK
              A deadlock was detected or the value of thread specifies the calling thread.

hahaite의 이미지

pthread_join 의 리턴값 체크는 생각도 못했네요.

결국 위 코드는 잘못된 코드네요. (EDEADLK) 값을 뱉어내더군요.

한 수 배우고 갑니다.

감사합니다.

^^