Related to this question, what is the best practice for naming a mutex? I realize this may vary with OS and even with version (esp for Windows), so please specify platform in answering. My interest is in Win XP and Vista.
-
If mghie's answer in that question was repeated here, I'd probably vote for it. – Stephen Denne Jan 21 '09 at 06:52
5 Answers
A really safe name for a global mutex is <a description> + <a GUID>
:
MyApp Single Instance Mutex : {c96f7db4-d743-4718-bef0-8533a198bcca}
By using a name like this there is absolutely no chance someone else will use the same mutex name as your mutex.
Sniffing around with process explorer, you can see that GUIDs are used in a few places, though in general they are not used. A pattern that does emerge though is that the word "mutex" is used quite a lot and Microsoft seem to like using capitols.

- 8,370
- 15
- 50
- 83

- 128,308
- 78
- 326
- 506
-
3
-
5
-
.NET assemblies already have a GUID associated with them; makes sense to just use ID, from the Assembly Info. – BTownTKD Oct 26 '17 at 14:36
-
@BTownTKD What if your assembly uses different mutexes in different places in the code? – Neo Jan 19 '22 at 06:49
-
@Neo once you've created a sufficiently-unique name for your process, you could simply append another suffix to differentiate between multiple mutexes inside that process. – BTownTKD Jan 20 '22 at 15:36
-
@BTownTKD That's more complex than the answer given, and achieves nothing additional. The answer given is simple and differentiates between multiple mutexes inside the process. – Neo Jan 20 '22 at 22:04
Suggestion:
Incorporate the object type (Mutex in this case) and application Namespace into the unique name. This will generally be safe. If you want to really be safe then append a Guid as well.
Example:
string mutexName = "MUTEX: Skyz.Messaging.ThreadPooling.MyAppSingleInstance";
Advantages:
By creating a naming convention for your apps you make it easy to manage many object names, create more readable code and will make it very easy for existing and future developers to understand the code.
Tip:
Instead of using a Mutex Directly in your code write a reusable wrapper class that can make the code more maintainable in case you ever want to change the implementation or add a tweak. Remember to Remove the Mutex using a disposable pattern or you will have issues!
using (SingletonProcess singletonProcess = new SingletonProcess("MUTEX: Skyz.Apps.MessagingQueue.InstanceMarker"))
{
if (singletonProcess.IsDuplicateInstance)
{
ConsoleWriter.WriteColorLine("An instance of the ExporterService exists, you cannot start a second instance.");
return
}

- 73
- 1
- 4
A google search of CreateMutex samples reveals that "MyMutex" is the most common mutex name chosen.
Therefore you should name your mutex "NotMyMutex" to guarantee uniqueness.

- 750
- 4
- 10
-
4If enough people follow that advice, it soon won't work anymore. – S.L. Barth is on codidact.com Oct 06 '12 at 08:12
You could combine a description of what you're protecting against with the word "Guard"

- 36,219
- 10
- 45
- 60
I haven't used GUID's in the past, but I'm starting to think its a good idea - if you think about all the developers in the world working of different software.
Unless you are thinking up quite obscure names that you can be assured are unique, you should think about GUID's.

- 332
- 3
- 13