pthread_join이 return 되지 않는 현상.

pkh0104의 이미지

안녕하세요.
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
          , NULL, lv_2_thread, (void*)&index);
          pthread_cond_wait(&cond
            , &cond_mutex
              );
              do_something_with_data();
              pthread_mutex_unlock(&cond_mutex
                );
                int* ret;
                pthread_join(lv_2_thread_id
                  , (void*)&ret);
                  }
                  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;
                              }
                              }
                              }

                              해답이나 조언을 부탁 드립니다.
                              감사합니다.

                              댓글 달기

                              Filtered HTML

                              • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
                              • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
                              • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
                              • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

                              BBCode

                              • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
                              • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
                              • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
                              • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

                              Textile

                              • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
                              • You can use Textile markup to format text.
                              • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

                              Markdown

                              • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
                              • Quick Tips:
                                • Two or more spaces at a line's end = Line break
                                • Double returns = Paragraph
                                • *Single asterisks* or _single underscores_ = Emphasis
                                • **Double** or __double__ = Strong
                                • This is [a link](http://the.link.example.com "The optional title text")
                                For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
                              • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
                              • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

                              Plain text

                              • HTML 태그를 사용할 수 없습니다.
                              • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
                              • 줄과 단락은 자동으로 분리됩니다.
                              댓글 첨부 파일
                              이 댓글에 이미지나 파일을 업로드 합니다.
                              파일 크기는 8 MB보다 작아야 합니다.
                              허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
                              CAPTCHA
                              이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.