Using the Windows Mutex functions to make an application one instance I'm wondering how to tell if the Mutex object, if it exists, is 'owned' or not so I can ignore it being a valid object should the previous instance have crashed?
Asked
Active
Viewed 879 times
0
-
A program that crashed and was terminated by Windows cannot own a mutex. Googling "windows single instance mutex" gives you a hundred fifty six thousand hits, surely you can find one that you can live with. – Hans Passant May 26 '14 at 15:56
-
Thanks for that, I know a program that crashed cant own a mutex but the mutex can still exist in its abandoned state and it is this state I want to check for. – Col_Blimp May 26 '14 at 16:00
-
No, clearly there's only one process that can own the mutex, the point of a single instance app. It is not "abandoned" on a crash, it is no more. It ceases to be. It is a pushing up the daisies. It is pining for the fjords, it is a dead parrot. – Hans Passant May 26 '14 at 16:11
-
Look, thanks for the attitude and sarcasm however I'm simply asking a question based on the notes in the 'Using Mutex Objects' that clearly states 'If a mutex is abandoned, the thread that owned the mutex did not properly release it before terminating. In this case, the status of the shared resource is indeterminate' and it is this state I want to check for as the title of the question clearly states. – Col_Blimp May 26 '14 at 16:16
-
That doesn't apply in this situation. If the thread that owns a mutex exits, the mutex becomes abandoned. However, if the *process* exits, the mutex is deleted - unless another process has a handle to it, of course. – Harry Johnston May 29 '14 at 08:14
-
I got it thanks, Microsoft documentation leads a lot to be desired. – Col_Blimp May 29 '14 at 09:15
1 Answers
1
Your main goal is to have a single instance of the application.
- You could create a mutex without acquiring it, set
bInitialOwner
toFALSE
, so you can use as a label. - On the start up, check if the mutex exits, if so, cleanup, e.g. notify the existing process, and exit.
- If not, create one without acquiring it.
for example:
HANDLE Mutex;
DWORD Error;
Mutex = CreateMutex(NULL, FALSE, TEXT("UniqueMutexName"));
Error = GetLastError();
if(Mutex != NULL && Error == ERROR_ALREADY_EXISTS)
{
/* another instance running */
CloseHandle(Mutex);
ExitProcess(0);
}
else if(Mutex == NULL)
{
/* different error */
SetLastError(Error);
}
...
CloseHandle(Mutex);
If you want to check if the mutex is owned, you can call WaitForSingleObject
with zero timeout:
switch(WaitForSingleObject(Mutex, 0))
{
case WAIT_ABANDONED:
/* similar to the bellow, but be careful with this one, if
* there's some protected shared data it may left corrupted */
case WAIT_OBJECT_0:
/* was not acquired, you just acquired it */
ReleaseMutex(Mutex);
break;
case WAIT_TIMEOUT:
/* already owned */
break;
default:
/* some error */
}
If the process was terminated or crushed without calling CloseHandle
, the system will close the handle automatically, CreateMutex
:
Use the
CloseHandle
function to close the handle. The system closes the handle automatically when the process terminates. The mutex object is destroyed when its last handle has been closed.
-
If you check for ownership, you risk acquiring ownership, so you have to be prepared to release the ownership if you acquire it. – Remy Lebeau May 26 '14 at 20:58