Click here to Skip to main content
11,932,103 members (61,528 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


1 bookmarked

How to avoid multiple instances of your Windows application

, 27 Sep 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
I tend to use boost::interprocess::named_mutex for the same thing. The equivalent code is something like:using boost::interprocess;const char *application_name = "Application Name";int main()try{ struct named_mutex_killer { ~named_mutex_killer() {...
I tend to use boost::interprocess::named_mutex for the same thing. The equivalent code is something like:

using boost::interprocess;
const char *application_name = "Application Name";
int main()
    struct named_mutex_killer
        ~named_mutex_killer() { named_mutex::remove( application_name ); }
    named_mutex instance_lock( create_only, application_name );
    std::cout << "Only instance of application started..." << std::endl;
catch( interprocess_exception &e )
    std::cout << "Running second instance of application" << std::endl;

It also works on FreeBSD and Linux (at least the versions I've tried) if that sort of thing's important to you.

It's slightly annoying that you have to use an auxillary structure to clean up the mutex. ~named_mutex() doesn't remove the underlying system mutex, it just makes sure that the mutex with the name is unlocked.

And to amplify the original author's point you can do it with any shared resource between processes - semaphores, file locks, you name it, not just mutexes although on pthread systems they tend to be the most primitive.




This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior)
United Kingdom United Kingdom
I've been programming since 1985 - starting with Fortran 77, then moving onto assembler, C and C++ in about 1991. I also know enough Java and Python to read code but you probably wouldn't want me writing it.

I've worked in a wide variety of application areas - defense, banking, games and security with the longest stints being in security. I also seem to end up programming devices far too often. This time I'm programming terahertz band body scanners.

You may also be interested in...

Comments and Discussions

GeneralMy vote of 2 Pin
Chris Hills26-Nov-13 7:51
memberChris Hills26-Nov-13 7:51 
GeneralRe: My vote of 2 Pin
Aescleal26-Nov-13 8:11
memberAescleal26-Nov-13 8:11 
GeneralRe: My vote of 2 Pin
Chris Hills27-Nov-13 0:13
memberChris Hills27-Nov-13 0:13 
GeneralRe: My vote of 2 Pin
Aescleal27-Nov-13 1:03
memberAescleal27-Nov-13 1:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.151126.1 | Last Updated 27 Sep 2010
Article Copyright 2010 by Aescleal
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid