Re: WaitForSingleObject() will not deadlock



File system drivers also use Executive Resources, a multi-reader single-writer lock
mechanism.
joe

On Sun, 1 Jul 2007 21:37:36 -0700, "Alexander Grigoriev" <alegr@xxxxxxxxxxxxx> wrote:

My guess that recursive mutexes may be handy to work on some tree-like
structures. File system drivers may be using them heavily.

"Doug Harrison [MVP]" <dsh@xxxxxxxx> wrote in message
news:66hg83t2evoskiqgf9pcb7clf6frr1rk8k@xxxxxxxxxx
On Sun, 1 Jul 2007 17:01:07 -0700, "Alexander Grigoriev"
<alegr@xxxxxxxxxxxxx> wrote:

Anybody tell me, why any other variants besides a recursive
one were invented?

If I may turn the question around, here are some arguments against
recursion:

1. Recursion makes it easy to hold a mutex across function calls and may
even encourage it, which is at odds with the goal of holding a mutex for
as
short a time as possible.

2. People who routinely take advantage of recursive mutexes tend to
develop
lax locking protocols and may end up writing code that has subtler races
than they'd get had they been limited to non-recursive mutexes.

3. The possibility of recursion makes it harder to implement algorithms
that requiring releasing the mutex. For example, a thread that holds a
mutex and needs to release it can't just unlock it, but must instead
unlock
it the correct number of times, which in Windows means keeping track of
the
lock count itself, since you can't query this property. (I wouldn't feel
comfortable calling ReleaseMutex until it fails, and LeaveCriticalSection
returns void.)

4. This is a relatively minor concern, but supporting recursion is more
expensive than not supporting it.

--
Doug Harrison
Visual C++ MVP

Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.



Relevant Pages

  • Re: WaitForSingleObject() will not deadlock
    ... But the issue is how can you lock and traverse a potentially circular list with "node has ... circular lists, and it wasn't easy. ... The cost of supporting recursion led to the Kernel concept of a "fast mutex" which is used ...
    (microsoft.public.vc.mfc)
  • Re: WaitForSingleObject() will not deadlock
    ... circular list detection. ... if you try to lock the object to inspect ... the bit, and you already own the mutex, then you are able to acquire the lock ... hazard a guess that posix mutexes have ALWAYS had recursion as ...
    (microsoft.public.vc.mfc)
  • Re: msleep() on recursivly locked mutexes
    ... If you are recursing on a mutex you need to switch to some other method of doing things. ... Over time, it's my hope that we can eliminate quite a few sources of remaining lock recursion, but there are some tricky cases involving repeated callbacks between layers that make that harder. ... For example, in the socket/network pcb relationship, there's a lack of clarity on which side drives the overlapping state machines present in both sets of data structures. ...
    (freebsd-hackers)
  • Re: WaitForSingleObject() will not deadlock
    ... Recursion makes it easy to hold a mutex across function calls and may ... mutex and needs to release it can't just unlock it, ... This is a relatively minor concern, but supporting recursion is more ...
    (microsoft.public.vc.mfc)