pthread_join이 return 되지 않는 현상.
안녕하세요.
pthread를 이용하여 다음과 같은 Scheme으로 c++ programming 하고 있는데 이해 할 수 없는 현상이 생겨서 질문 드립니다.
thread#1 - loop2 - thread#1-1
/
main - loop
\
thread#2 - loop2 - thread#2-1
위와 같이 main은 일정 횟수 loop를 돌면서 2개씩 thread create,join (thread#1,2)을 반복하고하고,
이렇게 생성된 각 thread는 일정 횟수 loop2를 돌면서 하나의 thread create,join(thread#1-1,2-1)을 반복하게 되는 Scheme 입니다.
지금 처한 문제는 잘 실행되고 있는 도중에 Random하게 loop2 부분에서 pthread_join이 return되지 않아 Program이 진행되지 않고 멈춰있는 것입니다.
stderr을 이용한 Log 상으로는 분명 loop2에서 pthread_join에 진입하였고 해당 thread(thread#1-1 or 2-1)가 종료된 것 같은데 join에서 멈춰버리니 답을 못찾겠습니다.
환경은 Linux 2.6.9 gcc version 3.4.6 이고,
다음은 간략화한 Source Code입니다.
pthread_t lv_1_thread_id[2];
pthread_t lv_2_thread_id[2];
int thread_arg[2];
pthread_mutex_t mutex[2];
pthread_mutex_t cond_mutex[2];
pthread_cond_t cond[2];
main()
{
init_mutex_cond();
for(int i=0; i<2; i++)
{
thread_arg[i] = i;
pthread_create(&lv_1_thread_id[i], NULL, lv_1_thread, (void*)&thread_arg[i]);
}
for(int i=0; i<2; i++)
{
int* ret;
pthread_join(lv_1_thread_id[i], (void*)&ret);
}
}
void* lv_1_thread(void* arg)
{
int* temp = (int*)arg;
int index = *temp;
for(int i=0; i<3; i++)
{
pthread_mutex_lock(&mutex
write(index);
pthread_mutex_unlock(&mutex
pthread_mutex_lock(&cond_mutex
pthread_create(&lv_2_thread_id
pthread_cond_wait(&cond
do_something_with_data();
pthread_mutex_unlock(&cond_mutex
int* ret;
pthread_join(lv_2_thread_id
}
return NULL;
}
void* thread_lv_2(void* arg)
{
int* temp = (int*)arg;
int index = *temp;
while(1)
{
pthread_mutex_lock(&mutex
if(isdata())
{
read();
pthread_mutex_unlock(&mutex
pthread_mutex_lock(&cond_mutex
parse_data();
pthread_mutex_unlock(&cond_mutex
pthread_cond_signal(&cond
return NULL;
}
else
{
pthread_mutex_unlock(&mutex
}
if(timeout)
{
return NULL;
}
}
}
해답이나 조언을 부탁 드립니다.
감사합니다.
댓글 달기