pthread_cond_timedwait and state of mutex upon error return

pthread_cond_timedwait and state of mutex upon error return

Post by d_cymba » Sat, 16 Jun 2007 23:15:13


Hi All,

Reading the man page for pthread_cond_timedwait states that when
either the condvar is signalled, or the wait times out, the asociated
mutex is reaquired. It doesn't explicitly mention if the mutex is
reaquired after any other error return from the call. Is there any
guarantee as to whether the mutex is reaquired or not if
pthread_cond_timedwait returns anything other than 0 or ETIMEDOUT?

Thanks.

dc
 
 
 

pthread_cond_timedwait and state of mutex upon error return

Post by David Schw » Mon, 18 Jun 2007 04:30:37

On Jun 15, 7:15 am, " XXXX@XXXXX.COM " < XXXX@XXXXX.COM >


So long as their is no corruption and error codes are sanely detected,
any other error code would indicate that the 'pthread_cond_timedwait'
function had done nothing at all. That is, any sanity checks the
implementation is capable of doing, it will do before it acquires or
releases anything.

For example, if you call 'pthread_cond_timedwait' without holding the
mutex, and the implementation is smart enough to detect this and
return 'EPERM', you still don't hold the mutex. If you call
'pthread_cond_timedwait' holding the wrong mutex, and the
implementation is smart enough to detect this and return 'EINVAL', you
still hold the wrong mutex.

Note that if your program violates the POSIX interface contract, you
should not rely on the implementation to sanely detect this. You
should not violate the interface contract at all. Just because you
will get sane behavior *IF* the implementation detects the problem
doesn't mean the implementation *WILL* detect the problem.

DS

 
 
 

pthread_cond_timedwait and state of mutex upon error return

Post by alexk » Wed, 20 Jun 2007 07:02:57


FWIW, you're not supposed to call pthread_cond_timedwait without
holding
the mutex, but if the mutex is held before the call to
pthread_cond_(timed)wait,
it will be held when the function returns (error or no error.)