Jump to content
  • Advertisement
Sign in to follow this  
Red Ant

General question about mutexes

This topic is 4804 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Obviously the purpose of a mutex is (usually) to keep several threads from simultaneously messing with critical sections of code. However, what happens if one and the same thread locks a mutex and then tries to lock it again WITHOUT having released it beforehand? Would this hang the thread, or is the OS smart enough to see that the thread trying to obtain a lock on the mutex is already the owner of the mutex in the first instance?

Share this post


Link to post
Share on other sites
Advertisement
Any good OS should handle that situation gracefully, I would expect. I'm only familiar with the Win32 threading system, which does handle this situation well, but I would expect it to work on almost all other OSes as well. What the Win32 stuff usually does is expect one unlock for every lock, so if a single thread locks a mutex, and then locks it again, it needs to unlock it twice before the thread has truly unlocked it. Here's an excerpt from MSDN:
Quote:
The thread that owns a mutex can specify the same mutex in repeated wait function calls without blocking its execution. Typically, you would not wait repeatedly for the same mutex, but this mechanism prevents a thread from deadlocking itself while waiting for a mutex that it already owns. However, to release its ownership, the thread must call ReleaseMutex once for each time that the mutex satisfied a wait.

Share this post


Link to post
Share on other sites
It depends on the system. Some mutex objects support recursive locks and some don't. e.g. In Windows a Win32 user-mode mutex object supports recursion but there's a different type of mutex object available to kernel-mode that has higher performance but will deadlock if you try to acquire it more than once.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!