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



Relevant Pages

  • Re: Recursive mutex that can be waited upon (pthread)
    ... While you can use a mutex to avoid that data is changed, for me having a mutex does not mean that data is not changed, it only means that data is not changed by a different thread. ... My own thread may of course change the data, hence functions I call may want to change the data and if they do so, they must be sure that these changes are atomically, hence they must lock the object and they simply can't rely that I locked the object before -> thus I need recursive locks. ... Then I could as well throw out threads of my code and just use a single thread going through an event queue. ... you have a predicate condition on an invariant. ...
    (comp.programming.threads)
  • [patch 6/8] mutex subsystem, core
    ... mutex implementation, core files: just the basic subsystem, no users of it. ... straightforward mutexes with strict semantics: ... + * Block on a lock - add ourselves to the list of waiters. ...
    (Linux-Kernel)
  • [patch 6/8] mutex subsystem, core
    ... mutex implementation, core files: just the basic subsystem, no users of it. ... straightforward mutexes with strict semantics: ... + * Block on a lock - add ourselves to the list of waiters. ...
    (Linux-Kernel)
  • [PATCH 4/8] adaptive real-time lock support
    ... The Real Time patches to the Linux kernel converts the architecture ... compromising the integrity of critical sections protected by the lock. ... while retaining both the priority inheritance protocol as well as the ... the RT Mutex has been ...
    (Linux-Kernel)
  • Re: WaitForSingleObject() will not deadlock
    ... You keep talking about needing to know the reference count. ... algorithm that uses a recursive mutex that cares in the slightest what the reference count ... attempts to lock appears to be isomorphic to a recursive lock, ... cycle detection until you reach the end of the list, ...
    (microsoft.public.vc.mfc)