Re: How to check if a mutex is still locked?



David Schwartz <davids@xxxxxxxxxxxxx> writes:

On Sep 10, 1:27 pm, Szabolcs Ferenczi <szabolcs.feren...@xxxxxxxxx>
wrote:

Let us suppose that two threads are being performed by different
engines of a dual core system, thread A and thread B. Furthermore, let
us suppose that `m->is_locked' is being true at the moment when thread
A performs query `IsLocked'

and thread B is the owner of MyMutex.

and locks `&m->mutex.' While line 4 is
being performed, another thread B tries to change `struct MyMutex *m.'

In particular, thread B has just unlocked MyMutex, and is now setting
is_locked to false to reflect that fact.

Well, hopefully that action is protected by `&m->mutex,' so thread B
is delayed and thread A continues with exclusive access inside the
critical region. At line 5 thread A releases the mutex. Right then
thread B enters the critical region and changes `m->is_locked' to
false. Eventually, thread A returns with boolean value true (which is
false).

What action can you take now in thread A?

Only the thread that holds the mutex may set m->is_locked to false.
This should be enforced in the 'unlock' function.

Thread B held the mutex and just released it in unlock() function.
Thread A got the answer (true) that was correct when IsLocked() was
called, but which is no longer correct.

What action can you take now in thread A?

This whole discussion is completely pointless anyway: only the
thread that locked the mutex may unlock it, so even if you could
reliably find out that someone owns the mutex, it is still not
legal to unlock it (unless it is owned by the current thread).

Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.
.



Relevant Pages

  • 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)
  • Re: How to check if a mutex is still locked?
    ... unlock it. ... Write a mutex class that has precisely the semantics you require. ... Your 'MyMutex' struct will probably need an internal ...
    (comp.programming.threads)
  • Re: A scoped lock/unlock implementation in C++.
    ... mutex_locker wrapping the same mutex, more than one thread won't share ... But beyond that it will still work with thread local mutex_locker instances, since no two threads can have a nonzero lock count anyway. ... When entering a locked region, if the last entry is negative, you know ... In case of unlocks even the unlock count is insignificant. ...
    (comp.programming.threads)
  • Re: How to check if a mutex is still locked?
    ... Shared resource should only be handled inside critical regions ... particular mutex mentioned in the initial message. ... lock the_super_mutex ... unlock the_super_mutex ...
    (comp.programming.threads)
  • Pthreads: how to insure mutex is unlocked when a thread dies.
    ... Anyway, I was planning to have this thread lock a mutex when it starts, ... unlock it when it's done, e.g., when the application notifies it to ... I'm concerned that if the thread dies before the application notifies ...
    (comp.programming.threads)