pthread_cond_wait에서 빠져나오질 못해서..질문좀 드릴게요.
글쓴이: emeraldrhapsody / 작성시간: 수, 2014/07/23 - 11:21오전
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에서 무한대기를 타버리는데 어떤 동작을 잘못 지정한건지 알려주시면 감사하겠습니다.
Forums:


wait 하면 signal 못날리죠. 다른 스레드에서
wait 하면 signal 못날리죠.
다른 스레드에서 날려줘야 합니다.
이해가 잘 안가서 그러는데요
start할때 signal을 날렸고 stop에서 다시 signal을 날리는데 다른스레드에서 날리라고 하신게 어떤 의미인가요?
wait해있을 때 signal을 날려야
wait해있을 때 signal을 날려야 합니다.
그런데 싱글 스레드면 wait 해있을 때, signal을 날릴 수가 없어요.
그렇다면 싱글쓰레드에선 cond_wait를 못쓰는거죠?
만약
void *aa(void *pArg)
{
}
void *bb(void *pArg)
{
}
이렇게 두개가 있다면 어떤식으로 작성하면 되나요?
aa pthread_mutex_lock(&mutex)
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를 먼저 수행해야 합니다.
답변 감사합니다!!
^^
while(1) 루프내에서
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()에서 원하는 세마포를 얻지 못해서 멈추는 듯 보이네요.
옮겨적는 과정에서 잘못 옮겨적었네요..
죄송합니다. 근데 그런 문제는 아닌듯 하구요..딱 wait이후로 블럭되버리는거 같습니다.
댓글 달기