I have a mutex created with CreateMutex, and then I use WaitForSingleObject to wait on some thread until such time that the Mutex is released and take ownership of it (with no timeout).
So, if the function returns WAIT_OBJECT_0, I can go on my marry way and execute the threads’ code.
However, in case the function returns WAIT_ABANDONED, can I still execute the code, expecting that any other thread calling WaitForSingleObject on my Mutex is going to get stuck until I release?
In case the owning thread terminates without releasing the Mutex, I want the calling thread to take ownership and things to keep going as usual.
EDIT: what is confusing to me is that MSDN says the state of the mutex in case (2) is unsignaled as opposing to state (1) where it is signaled. What does that mean for me?
If you get
WAIT_ABANDONEDback, that means you have taken ownership of the mutex. The problem is that you don’t know the state of the resource that is being protected by it.For example, let’s say Thread X gets a mutex and starts manipulating the state of something protected by that mutex, and then gets killed. The state is unknown and the mutex is unheld. When you go to wait on that mutex, you will receive it, but you won’t know that state.