How do you handle Mutex?

Discussion in 'Game Development (Technical)' started by Gilzu, Jun 2, 2006.

  1. Gilzu

    Moderator Original Member

    Joined:
    Jul 27, 2004
    Messages:
    368
    Likes Received:
    8
    AFAIK, Mutex is a good tool to limit instances of your game. I simply want to avoid a case where a user accidently runs more than one copy of my game at once.

    So i used a Mutex (tried both window's and SDL_mutex) and there's seems to be some nasty, annoying bug with SDL / SDL_mixer that makes your game crash. Took me two days just to figure out the reason for it.

    Searching for a solution for the bug didn't help much - there's seems to be some documentation about this bug, but no real bypass / solution or even a hint for one.

    I have considered using the old method of creating a file and disallowing any other instance to continue while this file is around - but its disadvantage that once your game crashes, you need to manually remove this file.

    so... does anyone have experience with SDL & SDL_mixer while using Mutex? or maybe a better method?
     
  2. ggambett

    Moderator Original Member Indie Author

    Joined:
    Jul 26, 2004
    Messages:
    1,982
    Likes Received:
    6
    Mutex ("MUTual EXclusion") objects are a synchronization primitive used to prevent simultaneous access to a resource or a section of code. Theoretically you can do what you want with a mutex, but only if you use a system-wide mutex - otherwise, both copies of your game will have a different mutex. This is probably unrelated to your SDL_Mixer crash (I use SDL_Mixer and SDL_Mutex without problems) but if it didn't crash, it probably wouldn't work either.

    In Windows you can manually create a global mutex (using the Windows API, not SDL). A trick I'm using but I'm not entirely happy with is registering a window class with a game-specific name (instead of SDL_App), and checking for that instance on startup. If it's already registered, the game is running.
     
  3. Savant

    Original Member

    Joined:
    Feb 8, 2005
    Messages:
    1,674
    Likes Received:
    0
  4. mahlzeit

    Original Member

    Joined:
    Sep 26, 2004
    Messages:
    852
    Likes Received:
    1
    Another trick you can use is to open a server socket on a predefined port number and listen for incoming messages. If you can't open the socket that means another instance of your app is already runnings, so connect to that port and send it your command line parameters, then exit. Works on all platforms. Disadvantage is that you need to use a port number that no one else is using.
     
  5. princec

    Indie Author

    Joined:
    Jul 27, 2004
    Messages:
    4,873
    Likes Received:
    0
    I think because this question is about a specific technical trick whereas the other question was extremely vague and the sort of thing that has many answers and indicative of someone a bit clueless who might do well to ask in some more "my first program" forum like Gamedev.

    Cas :)
     
  6. BarrySlisk

    Original Member

    Joined:
    Aug 18, 2004
    Messages:
    143
    Likes Received:
    0
    I don't get it either. I think the description for the forum should be more specific if technical questions are not allowed in the technical forum :) I can understand why some people get confused and post technical questions.

    Gilzu:
    If only you used C#, then I would have some code which loops through the running processes on the machine and if the process with the same name as my own program is running it would just quit. I'm sure the same thing can be done in C++ or whatever you are using. The open socket method screams HACK!
     
  7. Donavon Keithley

    Original Member

    Joined:
    Aug 5, 2004
    Messages:
    110
    Likes Received:
    0
    What was the problem with CreateMutex?

    This is what I use at the head of my WinMain:
    Code:
    CreateMutex(NULL, TRUE, _T("MyAppSingletonMutex"));
    if (GetLastError() == ERROR_ALREADY_EXISTS)
      return 0;
    
    Pretty simple, and it hasn't given me any problems yet.

    (Like the docs say, a malicious app could conceivably generate the mutex and block you from running, but I'm not paranoid enough to care.)
     
  8. ggambett

    Moderator Original Member Indie Author

    Joined:
    Jul 26, 2004
    Messages:
    1,982
    Likes Received:
    6
    Sure, I'll try to explain it very slowly. This thread asks whether a relatively complex sinchronization primitive is the best way to enforce having a single instance of a game. This thread is also by Gilzu, an old timer who has contributed a lot to the forums over the years.

    The other thread is by a newcomer (nothing wrong with that though) who started three very newbie-level threads - "Are indie games profitable?", asking "are there any famous and successful indie game developers known?" which means he didn't do any kind of search in the forum or basic research or else he would have heard of a certain PopCap. Lazy, but I'm cool with that. Second thread is a very basic question about paths - which has also been discussed at length previously (see Leper's posts). Lazy and redundant, but I'm cool with that too. The third is even more basic and clearly not suitable for this forum, so I politely explained why, pointed him to better resources, and locked the thread.

    If you need further education or have any more questions, I'll be happy to answer them via PM.
     
  9. Savant

    Original Member

    Joined:
    Feb 8, 2005
    Messages:
    1,674
    Likes Received:
    0
    No need to get snarky. I wasn't the only one confused.

    OK, so it's evaluated on a case by case basis adjusted by how interested you are personally in talking about the topic at hand.

    I understand. Thanks!
     
  10. Gilzu

    Moderator Original Member

    Joined:
    Jul 27, 2004
    Messages:
    368
    Likes Received:
    8
    This exact code causes the SDL / SDL_mixer Bug.
    As I mentioned in my first post, I have tried this method and SDL_mutex method, and still this annoying bug occurs :(
     
  11. mahlzeit

    Original Member

    Joined:
    Sep 26, 2004
    Messages:
    852
    Likes Received:
    1
    Not really. Using sockets is a very common way of doing interprocess communication. It may be overkill if you can get away with using a mutex, but if you're going to be passing around commandline parameters and whatnot, you'll have to use some form of IPC anyway. Also, not all programming languages can create system-wide mutexes. ;)
     
  12. Fabio

    Original Member

    Joined:
    Sep 30, 2005
    Messages:
    499
    Likes Received:
    0
    The best way to do interprocess communication in Win32 is IMHO a shared DLL.
     
  13. Applewood

    Moderator Original Member Indie Author

    Joined:
    Jul 29, 2004
    Messages:
    3,859
    Likes Received:
    2
    FindWindow()

    If you find your window, quit.

    Or did I misunderstand - there's a lot of technical stuff above but if you wanna make sure only one version of your app is running, just look for it when you run :)
     
  14. MikeVitt

    Original Member

    Joined:
    Dec 16, 2005
    Messages:
    48
    Likes Received:
    0
    This is probably obvious, but you can also then take that window handle and force the game to the foreground if minimized or behind another window like Explorer. Otherwise, the user might think there is an error when nothing happens.
     
  15. Applewood

    Moderator Original Member Indie Author

    Joined:
    Jul 29, 2004
    Messages:
    3,859
    Likes Received:
    2
    I forgot about this - good advice.

    My engine used to do this for me automatically, but I disabled it a while back when I actually wanted to run 4 copies at once to test some networking code. Best to put it on a switch I think.
     
  16. JasonD

    Original Member

    Joined:
    May 22, 2006
    Messages:
    14
    Likes Received:
    0
    Ouch.

    I find it interesting to mention a point but immediately take it back with "but that doesn't matter". The fact that you state it shows it does matter. Taking it back shows you know, logically, it shouldn't matter.

    ...and, apparently, it also matters who is asking the question. I am at a loss for words at this... especially after I just finished commending everyone for allowing the Pirate Software thread to continue.
     
  17. dbehnke

    Original Member

    Joined:
    Mar 17, 2005
    Messages:
    4
    Likes Received:
    0
    At the top of WinMain:

    Code:
    {
    		HWND hWnd = FindWindow( szClassName, NULL );
    		if ( IsWindow( hWnd ) )
    		{
    			HWND hWndPopup = GetLastActivePopup( hWnd );
    			if ( IsWindow( hWndPopup ) )
    				hWnd = hWndPopup;
    
    			SetForegroundWindow( hWnd );
    
    			if ( IsIconic( hWnd ) )
    				ShowWindow( hWnd, SW_RESTORE );
    
    			return 0;
    		}
    	}
    Use a GUID (guidgen.exe) to *reasonably* (not totally) ensure your classname is unique.
     
  18. Fabio

    Original Member

    Joined:
    Sep 30, 2005
    Messages:
    499
    Likes Received:
    0
    Microsoft has the merit of having introduced non-determinism in the computer world. :rolleyes:
     

Share This Page

  • About Indie Gamer

    When the original Dexterity Forums closed in 2004, Indie Gamer was born and a diverse community has grown out of a passion for creating great games. Here you will find over 10 years of in-depth discussion on game design, the business of game development, and marketing/sales. Indie Gamer also provides a friendly place to meet up with other Developers, Artists, Composers and Writers.
  • Buy us a beer!

    Indie Gamer is delicately held together by a single poor bastard who thankfully gets help from various community volunteers. If you frequent this site or have found value in something you've learned here, help keep the site running by donating a few dollars (for beer of course)!

    Sure, I'll Buy You a Beer