pthread_cond_wait에서 뮤텍스를 사용하는 이유?
글쓴이: mips / 작성시간: 화, 2008/05/13 - 3:39오후
pthread_cond_wait를 사용하는 코드를 보면 뮤텍스로 감싸놓은 경우가 있는데,
꼭 필요한 것인가요?
예를 들어서, 임계 영역이 따로 없고 아래와 같은 코드가 있다고 하면...
pthread_mutex_lock(&m); pthread_cond_wait(&c, &m); pthread_mutex_unlock(&m);
pthread_mutex_lock(&m)과 pthread_mutex_unlock(&m) 은 없어도 상관 없는 것 아닌가요?
그리고,
pthread_con_wait 함수 내부에서 사용되는 뮤텍스(여기서는 m)의 경우, 조건 변수에 대해 경쟁 조건을
방지하기 위해서 사용하는 것이라고 하는데, 위와 같은 코드에서 pthread_mutex_lock에 의해서
이미 락이 걸린 상태인데 어떻게 pthread_cond_wait 함수에서 다시 같은 뮤텍스에 대해서 락을
걸 수 있을 까요? (pthread_cond_wait에서 다시 락을 시도하면 데드락이 걸리는게 맞는것 같은데...)
Forums:


NAME
NAME pthread_cond_timedwait, pthread_cond_wait - wait on a condition ...중략... DESCRIPTION The pthread_cond_timedwait() and pthread_cond_wait() functions shall block on a condition variable. <span>They shall be called with mutex locked by the calling thread or undefined behavior results</span>. These functions <span>atomically release mutex and cause the calling thread to block on the condition variable cond; atomically here means "atomically with respect to access by another thread to the mutex and then the condition variable"</span>. That is, if another thread is able to acquire the mutex after the about-to-block thread has released it, then a subsequent call to pthread_cond_broadcast() or pthread_cond_signal() in that thread shall behave as if it were issued after the about-to-block thread has blocked. ...중략... COPYRIGHT Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at <a href="http://www.opengroup.org/unix/online.html " rel="nofollow">http://www.opengroup.org/unix/online.html </a> . IEEE/The Open Group 2003 PTHREAD_COND_TIMEDWAIT(3P)OTL
댓글 달기