This solution works in Windows environment under Visual Studio.
I don't know if there is a Linux equivalent.
I use a counter common for all processes.
LONG gs_nCtApp = -1;
#pragma comment(linker, "/section:counter,rws")
Each time the program enter in
, it checks the counter.
If its value is greater than
, then another process is running.
Else, the counter is incremented.
bool bFirstInstance = (InterlockedIncrement(&gs_nCtApp) == 0);
MessageBox(NULL, _T("One Instance Is Already Running."), _T("Information"), 0);
There is no need to decrement at the end of the first process because when it will end, the image will be unloaded from memory.
So the next time it will be launched, it will be reset.Note: This solution is not from me, but I can't remember where I saw it. Sorry to the author...