Link to home
Start Free TrialLog in
Avatar of supersnoop
supersnoop

asked on

Equivalent of waitpid( -1, *stat_loc, WNOHANG ); in windows. Thread Safe

Hi,

I want to implement an equivalent of wait/waitpid in Windows. The problem that I am facing here is that I have to wait for all process that have been created till this point in time. There are two threads which are running, one thread keeps on creating processes and the other thread waits for all the process that have been created. Windows provides WaitForMultipleObjects() methods but that will not suffice as I cannot add more processes to the set of handles for which the second thread is waiting. Also there is a question of thread safety. A direct equivalent or a strategy to get similar function is required.
Avatar of AlexFM
AlexFM

Please describe what do you want exactly. One thread creates some number of processes. Another thread waits when all these processes finished.
Why WaitForMultipleObjects is not solves this problem?
Avatar of supersnoop

ASKER

There are two threads which are running parallely. One thread (Say T1) responsibilty is to spawn new processes and the other thread (say T2) is waiting for signals from these threads. As sonn as T2 gets a signal from any of the process being waited on, it will report the status of the process, however it will continue to read signals from the processes. I will not know before hand the number of processes I want to create, as the creation of processes is dynamic on user request. WaitForMultipleObjects does not solve my problem as I would like to add new processes to the list of processes being waited for after I call the WaitForMultiple objects.
Still don't understand exactly what you need: what is "signal from any of the process". Second thread reports about finishing of each process?

WaitForMultipleObjects has bWaitAll parameter. If it is FALSE, function returns if at least one of the processes finished. Notice that next time you can call WaitForMultipleObjects with another (updated) list of project handles.

List of processes launched by thread1 may be kept in global container (like std::list) protected by critical section. Thread 1 adds new elements to it. Thread 2 removes them.

One of synchronization objects on which WaitForMultipleObjects waits in thread 2 should be event set in thread 1 when new process is launched. When this happens, WaitForMultipleObjects should exit and called again with updated list of processes.

Pseudo-code.
Thread 1:

while (...)
{
    do something...
    create process
    add process handle to global list
    set event "Process is created"
}


Thread 2:

while (...)
{
     WaitForMultipleObjects(<all process handles from list + event "Process is created">, bWaitAll = FALSE)

     if ( one of processes is signaled )
     {
          report
          remove process handle from list
          continue (wait again)
      }

     if ( "Process is created" event is signaled )
     {
           continue (wait on refreshed list of processes)
     }
}
Hi,

I am sorry about not adding comments before hand. Actually I found the answer myself about the equivalent functionality. There is a function in the new SDK called RegisterWaitForSingleObject wich does exactly what was required. Anyways thanks for the comments on WaitForMultipleObjects, only after a lot of deliberation we found that WaitForMultipleObjects was not suficing our requirement,
ASKER CERTIFIED SOLUTION
Avatar of ee_ai_construct
ee_ai_construct
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial