You have a mutex race. You are hoping for the following sequence:
child: create mutex parent: open mutex child: destroy mutex
But what could happen
child: create mutex child: destroy mutex parent: open mutex (fails because mutex is destroyed)
I cannot fully understand what your ultimate goal is, but I have a suspicion that the event is actually what you are looking for.
In parent:
- Create a named event.
- Set an event without an alarm.
- Create a child process.
- Wait until the event is signaled.
The child has:
- Do some processing.
- Open a named event.
- Set an event for signaling, thereby freeing the parent from his expectation.
At a very high level, the code you need will look like this:
Parent
Event = CreateEvent(nil, True, False, EventName); //create it manual reset, set to non-signaled ShellExecEx(....); WaitForSingleObject(Event);
Child
Event = CreateEvent(nil, True, False, EventName); //do stuff SetEvent(Event);
I did not enable error checking. I am sure you can add some. You may also find that the event wrapper class in SyncObjs more convenient.
Finally, your code has a busy cycle. This is almost never a solution to a problem. If you ever find yourself writing a busy cycle, you should take this as a signal of a bad design. The fact is that in your code, if it could be made to work, the parent process would burn 100% CPU usage while waiting for the child process.
David heffernan
source share