How to handle multiple concurrent instances at the same time?
===

That's the problem:
I don't like multiple instances of my program, that's why I've disabled them. My program opens a specific mime-type. In my system (Ubuntu 12.04), when I double click one of these files, this is executed:

    /usr/bin/myprogram /path/to/double/clicked/file.myextension

As I said, I don't like multiple instances, so, if the program is already running and the user chooses to open one of these files, a DBus message is being sent to the already instance so as to take care the opened file. So, if there's an already running instance and the user choose 3 files to open with my program and hit the [Enter] button, the system executes:

    /usr/bin/myprogram /path/to/double/clicked/file1.myextension
    /usr/bin/myprogram /path/to/double/clicked/file2.myextension
    /usr/bin/myprogram /path/to/double/clicked/file3.myextension

All of these instances detect the already running instance and sent the opened file to it. No problems at all, till now.

But, what if there isn't an already running instance and the user chooses to open 3 files altogether with my program? The system will call concurrently, again:

    /usr/bin/myprogram /path/to/double/clicked/file1.myextension
    /usr/bin/myprogram /path/to/double/clicked/file2.myextension
    /usr/bin/myprogram /path/to/double/clicked/file3.myextension

and each of these instances will realize that there's an already running instance, it will try to sent a DBus message to the already running instance and it will exit. So, all the 3 processes will do the same thing and nothing will run.

How can I avoid this problem?

PS: In order to detect if there are already running instances I implement the following code:

    bool already_runs(){
    return !system("pidof myprogram | grep \" \" > /dev/null");
    }
