[98] in Pthreads mailing list archive
Mutex question
daemon@ATHENA.MIT.EDU (Andreas Jung)
Mon Aug 21 06:08:42 1995
From: ajung@hssun5.cs.uni-sb.de (Andreas Jung)
To: pthreads@MIT.EDU
Date: Mon, 21 Aug 1995 11:30:09 +0200 (MET DST)
Hello,
I have a small problem understanding the use of condition variables
together with a mutex. The following example is the normal procedure
as described in the docs. func1 has locked the mutex and func2
is waiting to obtain the mutex:
int func1(int i)
{
pthread_mutex_lock(&m);
ready=0;
....
ready=1;
pthread_cond_signal(&cv);
pthread_mutex_unlock(&m);
return(1);
}
int func2(int i)
{
pthread_mutex_lock(&m);
while(!ready)
pthread_cond_wait(&cv, &m);
pthread_mutex_unlock(&m);
return(1);
}
If func1 has locked the mutex than func2 is blocked at the pthread_mutex_lock()
statement until func1 calls pthread_mutex_unlock(). So why is it neccessary to
to call pthread_cond_wait() when the lock is obtained by func2.
Any try to lock a mutex which is locked will block the thread in any case
until it is unlocked. Or is there any situation possible where 2 thread can
lock a mutex at the same time ?
Thanks in advance
Andreas Jung
--
------------------------------------------------------------------------
/// Andreas Jung, Klosterstr. 21, D-66125 Saarbruecken, Germany
/// Phone: +49-(0)681-302-3016 (work), +49-(0)6897-71959 (home)
\\\/// E-Mail: ajung@cs.uni-sb.de IRC: YET
\XX/ WWW: http://hssun4.cs.uni-sb.de/~ajung
------------------------------------------------------------------------