|Perl Monk, Perl Meditation|
parallelisation (how to wait on N threads)by erroneousBollock (Curate)
|on Nov 20, 2007 at 11:54 UTC||Need Help??|
Update: I, like the following commenters, don't think that threads are appropriate in this case, but I'll answer the question in case someone comes looking for an answer where threads would be appropriate.
My problem is that i don't see how i can check if ANY of the 4 workers has finished.Have a single control queue shared between the worker threads and the main thread.
Create a Thread::Queue object, which we'll call the "control queue". When you create the workers, pass that queue to each of the workers you create. When a worker is done it should enqueue a "worker N done" message into the "control queue".
In your main thread, once all the worker threads are started, do a blocking dequeue from the "control queue" in while a loop, where the loops ends when you've received the appropriate number of "worker N done" messages. (ie: You could receive other useful messages on the same queue).
In the body of the while loop, you can deal with what should happen when ANY of the workers finish (use a condition variable if you're only interested in doing so for the first).
After the loop, you can deal with what should happen when ALL of the workers have finished.
re: threads and system():
Also, you seem to be doing system() (which is fork() and exec()) inside the worker thread. Are you sure you understand the implications of mixing fork() with Perl threads?