tags:

views:

50

answers:

3

I'm using a named mutex to detect other instances of my application and exit accordingly, and found that there are two ways of doing this:

  1. Create the mutex; ignore the indication whether it already existed; try to acquire it; use the fact that acquire succeeded/failed.
  2. Create the mutex; use the indication whether it already existed.

I can't decide whether to acquire the mutex (and release on exit). On the one hand, acquiring+releasing even though it makes no known difference looks like cargo culting, but on the other hand the existence of a mutex object sounds like a side-effect of its actual intended functionality.

So, should I do #1 or #2 to detect if the app is already running?

A: 

I'm not sure of it but the named mutex may still exists if the program crashes and doesn't terminate properly. If so, the existence test will succeed whereas no other instance were running. Thus, I personnaly would prefer to try to acquire it ;-)

Seb
MSDN mentions that the handles held by terminated processes are released, and that the mutex is deleted when no handles to it remain, so I'm pretty sure this is not the case.
romkyns
A: 

#1 sounds the way you should go.

Create the mutex; ignore the indication whether it already existed; try to acquire it; use the fact that acquire succeeded/failed

Because your app launching code might be executed twice (on a resume or similar OS stuff), and the acquire will succeed even if the mutex is already existing as it was created by the same app id.

Pentium10
But the creation is an atomic operation - you always get the handle (assuming security checks succeeded), and can check whether the mutex already existed with a GetLastError. Also, how can my app launching code be executed twice - I'm pretty sure that would require the process to be started again.
romkyns
Generally speaking I've seen that on mobile devices. When the OS put's the app to stand by, or does GC. If you gain focus of the app again it is relaunched, and a bundle is passed to get previous state back.
Pentium10
Oh I see. Yeah, WinMobile likes to close/restart the apps at will, but I'm writing for the full-blown OS.
romkyns
+1  A: 

The indication that the mutex already existed is sufficient to let you know that there is at least one other process. There is no need to take the mutex for that.

But as long as you have the mutex, you can take it if you need to lock other instances out of some piece of code.

For instance, you can take the mutex until you get out of your initialization code. That way only one instance of your program can be in initialization at a time. If you take the mutex after opening it, the one that got the mutex first knows that no other instance is in its init code. But more importantly, the one that didn't create the mutex knows that the one that create is has finished initialization.

That way if instance 2 wants to talk to instance 1, it knows that instance 1 is ready to listen once it has been able to enter the mutex at least once. This works better if you create the mutex as initially signalled to be absolutely sure that the creator gets to be the first owner.

John Knoeller
Thanks for confirming this. Just a note that I wrote an example of how to do this in C# (not raw WinAPI!) in my answer here: http://stackoverflow.com/questions/778817/how-to-determine-if-a-previous-instance-of-my-application-is-running-c/2415639#2415639
romkyns