pthread_cond_wait에서 빠져나오질 못해서..질문좀 드릴게요.

emeraldrhapsody의 이미지

int CTest::Start()
{
  pthread_mutex_lock(&m_mutex);
 
  pthread_attr_setdetachstate( &m_attr, PTHREAD_CREATE_DETACHED );
 
  pthread_create( &m_hAAThread, &m_attr, AAThread, this );	
 
  pthread_cond_signal(&m_hAAThreadEvent);
 
  pthread_mutex_unlock(&m_mutex);
 
}
 
int CTest::Stop()
{
  m_bAAThreadAlive = FALSE;
 
  pthread_mutex_lock(&m_mutex);
 
  pthread_cond_signal(&m_hAAThreadEvent);
 
  pthread_mutex_unlock(&m_mutex);
 
  m_hAAThread = NULL;
}
 
void *AAThread(void *pArg)
{
  if ( pArg != NULL )
  {
     CTest *pFrm = static_cast<CTestThread*>(pArg);
     if ( pFrm != NULL )
     {
         pthread_mutex_lock(&pFrm->m_mutex);
         pthread_cond_signal(&pFrm->m_hAAThreadEvent);
         pthread_mutex_unlock(&pFrm->m_mutex);
 
         int i = 0;
 
         while(1)
         {
              pthread_mutex_lock(&pFrm->m_mutex);				
 
              BOOL bStop = pFrm->m_bAAThreadAlive;
              if ( bStop == FALSE )
                  break;
              printf("result: %d\n", i);
 
              i++;
 
              usleep(500 * 1000);
 
              pthread_cond_wait( &pFrm->m_hAAThreadEvent, &pFrm->m_mutex );
 
              pthread_cond_signal(&pFrm->m_hAAThreadEvent);
 
	      pthread_mutex_unlock(&pFrm->m_mutex);
         }         
     }
  }
  return NULL;
}

시그널 날리는데 까지는 문제가 없구요..그 뒤 wait에서 무한대기를 타버리는데 어떤 동작을 잘못 지정한건지 알려주시면 감사하겠습니다.

익명 사용자의 이미지

wait 하면 signal 못날리죠.
다른 스레드에서 날려줘야 합니다.

emeraldrhapsody의 이미지

start할때 signal을 날렸고 stop에서 다시 signal을 날리는데 다른스레드에서 날리라고 하신게 어떤 의미인가요?

익명 사용자의 이미지

wait해있을 때 signal을 날려야 합니다.
그런데 싱글 스레드면 wait 해있을 때, signal을 날릴 수가 없어요.

emeraldrhapsody의 이미지

만약

void *aa(void *pArg)
{
}

void *bb(void *pArg)
{
}

이렇게 두개가 있다면 어떤식으로 작성하면 되나요?

익명 사용자의 이미지

aa
pthread_mutex_lock(&mutex);
pthread_cond_wait(&cond, &mutex);
pthread_mutex_unlock(&mutex);

bb
pthread_mutex_lock(&mutex);
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);

wait가 signal보다 먼저 실행되어야 하므로, 반드시 aa를 먼저 수행해야 합니다.

emeraldrhapsody의 이미지

^^

라스코니의 이미지

while(1) 루프내에서
pthread_mutex_lock(&pFrm->m_mutex);
이 많이 호출되지 않나요? 지금은 exit 조건을 1초에 두번 체크하는 것 같은데요.....

pthread_mutex_lock(&pFrm->m_mutex);
이 while(1) 위에 있어야 할 것 같은데....

AAThread 맨 마지막에서 pthread_mutex_unlock(&pFrm->m_mutex);이 한번 만 호출되는데
확인해 보세요.

만약 Stop() 내의 m_mutex가 pFrm->m_mutex 와 같다면 Stop() 함수 내 첫번째 lock()에서 원하는 세마포를 얻지 못해서 멈추는 듯 보이네요.

emeraldrhapsody의 이미지

죄송합니다. 근데 그런 문제는 아닌듯 하구요..딱 wait이후로 블럭되버리는거 같습니다.

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.