Sign in to follow this  
supagu

mutex dead lock

Recommended Posts

seems my app is getting stuck in a mutex dead lock. And its being a pain the ass to debug. Now im rather new to multithreaded programming, when i create a mutex: mHandle = CreateMutex(0, false, 0); and lock it: WaitForSingleObject(mHandle, INFINITE); will this mutex only block if this handle has already been locked or will it also block if there is any other locked mutex?

Share this post


Link to post
Share on other sites
When you wait for a given mutex (mHandle), your thread will be blocked only if some thread has already locked that specific mutex. As soon as that mutex is unlocked, the waiting thread will be resumed.

Share this post


Link to post
Share on other sites
If you acquire the mutex multiple times in a single thread, though, you must release it an equal number of times.

Share this post


Link to post
Share on other sites
huh? how can it be acquired multiple times? i thought if you attempt to aquire a second time it blocks until the other frees it. Then obviously you will get a new lock which you must release when done...

Share this post


Link to post
Share on other sites
You can acquire the same mutex twice in the same thread. Think about what would happen if this was not allowed, and the second attempt to acquire "blocks until the other frees it".

Share this post


Link to post
Share on other sites
The only ways deadlock can be caused by using mutexes are:

1. You have some situation which acquires the mutex but never frees it.

2. You use more than 1 mutex, and you attempt to acquire them in inconsistent orders. (this is really just a special / common case of item 1).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this