Re: mutex lock timeout

From: David Schwartz (
Date: 08/23/03

Date: Fri, 22 Aug 2003 20:09:49 -0700

"Jan Cox" <> wrote in message

> I am using the pthread library, an old version that does not has
> pthread_yield and I would like to try to lock a mutex and timeout (give
> after say, 10 seconds if in the mean time I cannot lock it.
> Currently, the only solution I have is a combination of pthread_trylock
> microsleep (a select that times out) which is not exactly satisfactory -
> the microsleep is small the loop is tight and the cpu loaded, if the sleep
> is long I am loosing some other events.
> Any other way to do it?

    Don't use a mutex. A pthreads mutex is a fast, simple lock. It's a
primitive used to build higher-level lock structures should you need them. A
waitable (slow) lock is not a mutex. You can implement one fairly easily
with a mutex though.

    Should it be recursive or non-recursive? Should one thread be able to
lock it and another thread unlock it? Just decide exactly what you want and
code it.