-2

I would like for it to be impossible for my executable to have more than 1 instance at a time. I am currently attempting with SDL's SDL_CreateMutex() method but I can't get it to work.

int main(int argc, char* argv[]){

    SDL_mutex* mutex = SDL_CreateMutex();
    if(SDL_LockMutex(mutex) == 0){
        Game game;
        game.Initialize();
        game.Run();
        game.Destroy();
        SDL_UnlockMutex(mutex);
    } 
    SDL_DestroyMutex(mutex);

    return 0;
}
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
gabriel__
  • 20
  • 4
  • 1
    `SDL_CreateMutex()` can be used to prevent different threads within the same process to access a resource. It's not shared with other processes you start. Perhaps you should look at [boost interprocess mutex](https://www.boost.org/doc/libs/1_83_0/doc/html/interprocess/synchronization_mechanisms.html#interprocess.synchronization_mechanisms.mutexes.mutexes_interprocess_mutexes) – Ted Lyngmo Aug 21 '23 at 20:22
  • 1
    You need something that is shared across processes that you can lookup and see if it already exists. Here's a Windows-centric solution [How to Run Only One Instance of Application](https://stackoverflow.com/questions/4191465/how-to-run-only-one-instance-of-application) and some more advice [How to create a single instance application in C or C++](https://stackoverflow.com/questions/5339200/how-to-create-a-single-instance-application-in-c-or-c) – Retired Ninja Aug 21 '23 at 20:23

0 Answers0