Re: help w/ mutex

From: Gary Chanson (gchanson_at_No.Spam.TheWorld.net)
Date: 05/05/04


Date: Wed, 5 May 2004 15:08:41 -0400


"andrea catto'" <acatto@dataflight.com> wrote in message
news:uGWWVBsMEHA.2976@TK2MSFTNGP10.phx.gbl...
> Here is what I empiricaly found,
>
> Mutexes vs. Semaphores.
> Microsoft claims Mutexes are quicker, who knows how much though.
> Mutexes are thread-dependant, as already stated, if you try to 'get the
> ownership' of a mutex with in the same thread, even from a different
> function.
> that same call will return positively as if you indeed just got it when in
> reality you already got it at the first call.
> Mutexes are 'binary' therefore the idea is, either you get the ownership
or
> you dont, and may possibly wait until the current owner will release it to
> you.
>
> Semaphores are non binary (only), they are based on a count-down, you can
> use them in most cases to do the same things you could do with a mutex but
> you have to define more parameters so it's easier in most cases to use
> Mutexes.
> On top of it all the MAJOR difference is semaphores are not
> thread-dependant, they are merely count down dependant.
> if you create a semaphore with 'count down' 3 until it gets to 0 it won't
> trigger, and if you 'accidentally' call the waitforsingleobject twice on
the
> same semaphore within the same thread you are screwed.

    Another reason to not use a semaphore in place of a mutex is the
priority inversion problem. A semaphore can block a high priority thread
while a lower priority thread executes, effectively lowering the priority of
the high priority thread to that of the lower priority thread. Mutexes deal
with this problem by temporarily raising the priority of the low priority
thread to that of the higher priority thread while it is blocked.

-- 
-GJC [MS Windows SDK MVP]
-Software Consultant (Embedded systems and Real Time Controls)
- http://www.mvps.org/ArcaneIncantations/consulting.htm
-gchanson@mvps.org


Relevant Pages

  • [PATCH] kernel/cpuset.c mutex conversion comment fix
    ... The conversion of kernel/cpuset from semaphores to mutexes ... I didn't know offhand the state of read-write mutexes, ... - * We have two global cpuset semaphores below. ... - * zero, then only attach_task, which holds both semaphores, can ...
    (Linux-Kernel)
  • Re: help w/ mutex
    ... Mutexes are not any faster than semaphores, as they both involve switch to ... > thread-dependant, they are merely count down dependant. ... a CriticalSection is probably what you ...
    (microsoft.public.win32.programmer.kernel)
  • Re: [patch 04/15] Generic Mutex Subsystem, add-atomic-call-func-x86_64.patch
    ... don't forget to compare this with our existing semaphore ... this means that mutexes will be _FAR MORE EXPENSIVE_ on ARM ... We'd be far better off sticking to semaphores ...
    (Linux-Kernel)
  • Re: [PATCH] use spinlock instead of binary mutex in idt77252 driver
    ... defined (I don't know what current kernels do), ... You need to verify the headerfiles for the above. ... Does this work with mutexes? ... New code should not use semaphores, ...
    (Linux-Kernel)