Locking Mutex twice in one thread  
Author Message
Ameise





PostPosted: Visual C++ Express Edition, Locking Mutex twice in one thread Top

I was trying to acheive a flow pattern similar to this:

threadA {
while (1) {

mutB.lock

-work-

mutA.lock
if (worktodo) {
mutB.unlock
}
else {
haveLocked = 1
}
mutA.unlock
}
}

threadB {
while (1) {

-work-

mutA.lock
if (worktodo && isLocked) {
mutB.unlock
isLocked = 0
}
mutA.unlock

}
}threadA {

mutB.lock

-work-

mutA.lock
if (worktodo)
mutB.unlock
mutA.unlock

}

threadB {

-work-

mutA.lock
if (worktodo && mut.isLocked)
mutB.unlock
mutA.unlock

}

However, the second time that threadA tried to lock mutB, it just said "done" and let it go.

I assume that this was a safety built in to try to prevent users from creating deadlock, but as you see from my flow pattern, this is my intent.

How can I do this




Visual Studio Express Editions34  
 
 
Mike Danes





PostPosted: Visual C++ Express Edition, Locking Mutex twice in one thread Top

Indeed mutex (and critical section) can be aquired more than once by the same thread without blocking. Maybe you can use a semaphore instead of a mutex

But why do you want to create a deadlock Other than testing/demo purposes I can't see any useful use for a deadlock !