Re: Limiting to one instance

Jon Watte <[email protected]> Tue, 15 Aug 2006 10:51:23 -0700
Newsgroups gmane.games.devel.windows
Message-ID <[email protected]>

Alen Ladavac wrote:
> Personally, I've been using FindWindow(xyz, NULL) to find a main
> window by class name. Same principle (with same weaknesses as a named
> mutex), but doesn't require an extra object creation, as you probably
> already have a window, and you can use this to test even for foreign
>   

Using FindWindow() introduces a possible race condition in your 
application, because two instances starting up at the same time (because 
the impatient user double-clicked twice) MAY both first run the test, 
find that there's no other window, and then create the window.

Mutex creation name resolution is guaranteed by the kernel to be atomic, 
and thus there is no race condition; the first one in, gets it. Note 
that you don't actually then use the mutex at all; all you do is using 
the creation of the mutex as your existence test, given that a mutex is 
reasonably lightweight, and the kernel rules surrounding their lifetime 
are iron-clad. Of course, if you're worried about pranksters, consider 
that someone might create a mutex with the same name just to spite you :-P

Btw: you don't need to close the handle before exit, because the kernel 
can clean it up. Trust the kernel. The kernel is your friend!

Cheers,

             / h+


-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Gamedevlists-windows mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/gamedevlists-windows
Archives:
http://sourceforge.net/mailarchive/forum.php?forum_id=555