Acquiring multiple times the mutex from the same thread

Acquiring multiple times the mutex from the same thread

I have some questions for which I was not able to find an answer in the documentation.
1- What happens if a thread acquires a lock to a spin_mutex, then it invokes a method than tries to lock the same mutex again? Does the second lock do nothing? or does it meet a deadlock?
2- Now consider the spin_rw_mutex with a method that firstly acquires a reading lock to the mutex, then it invokes a method with a writing lock, without upgrading it. What happens? deadlock?
I think that basically I am looking for something that combines the recursive property with the r/w capabilities...


3 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

1. It will livelock, busy-waiting forever.
2. Same thing. You should definitely try to upgrade, instead.

A ReaderWriterMutex (referring to the concept) implies that you know when you already hold the mutex (for reading) when trying to acquire it again (for writing), so to me at least it seems a little strange to want to combine that with not knowing (recursiveness)...,

(other than for bad programming) This sounds like an opportunity for you to add a thread local variable that is used to indicate if your thread currently owns the lock.

__declspec (thread) myFooLockCount = 0;
if(myFooLock++ == 0) doFooLockHere();
if(--myFooLock == 0) doFooUnlockHere();

Jim Dempsey

Leave a Comment

Please sign in to add a comment. Not a member? Join today