I am working on a Linux application that uses streams of manufacturers and consumers. This is a fairly mature program, and I do not want to change the architecture more than necessary.
Production and consumer flows are linked through the expected lineup. This is a class implemented through std :: queue along with the variable condition and mutex.
Now I want consumer threads to be able to process the fork / exec child process and wait for the child process to complete, or the expected queue is not empty, whichever happens first. If the expected queue is not empty, then the child process must be killed. Edit:. Child processes are third-party applications that cannot be changed.
One possibility is to call pthread_cond_signal () in my condition variable when the child process exits, but how to do it? I cannot use the handler for SIGCHLD, at least not directly, because the man page says that pthread_cond_signal () cannot be used from the signal handler.
One possible way is to create a child process, and then start the thread to block waitpid () and finally pthread_cond_signal (). This seems a bit awkward: do I really need to spawn a thread to keep an eye on pid?
For mixing waitpid and select / poll / epoll there is Trick Trick . Is there an equivalent for mixing waitpid and variable conditions?
Note 1: In some implementations, SIGCHLD interrupts state variable wait functions. This is not portable, and if possible, I would rather not rely on this behavior.
Note 2: Since the condition variable is encapsulated in the wait queue class, I need to extend this class so that the application can signal the mutex. This is just a trivial implementation detail that I was silent about in my question.
source share