Re: mutex lock timeout

From: David Schwartz (davids_at_webmaster.com)
Date: 08/23/03


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


"Jan Cox" <someone_nobody@microsoft.com> wrote in message
news:bi5gs4$5eer8$1@ID-151088.news.uni-berlin.de...

> 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
up)
> 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
and
> microsleep (a select that times out) which is not exactly satisfactory -
If
> 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.

    DS