Re: How to check if a mutex is still locked?
- From: Paul Pluzhnikov <ppluzhnikov-nsp@xxxxxxxxxxx>
- Date: Mon, 10 Sep 2007 22:50:55 -0700
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.
.
- Follow-Ups:
- Re: How to check if a mutex is still locked?
- From: jonnie
- Re: How to check if a mutex is still locked?
- From: David Schwartz
- Re: How to check if a mutex is still locked?
- References:
- How to check if a mutex is still locked?
- From: GeertVc
- Re: How to check if a mutex is still locked?
- From: David Schwartz
- Re: How to check if a mutex is still locked?
- From: Szabolcs Ferenczi
- Re: How to check if a mutex is still locked?
- From: David Schwartz
- How to check if a mutex is still locked?
- Prev by Date: Re: cannot link -static with threads
- Next by Date: Re: How to check if a mutex is still locked?
- Previous by thread: Re: How to check if a mutex is still locked?
- Next by thread: Re: How to check if a mutex is still locked?
- Index(es):
Relevant Pages
|