in reply to Using threads to run multiple external processes at the same time

Actually a simple boss/worker model should be easy enough. This wont scale to multiple computers but its a starting point. Note that this code was torn from a larger fully functional threaded program I have. The code below is not complete and not even tested but it should demonstrate all the concepts you need.

For example, how can I avoid a race condition when I'm filling up the result data structure (for the table) if the results themselves come in asynchronously?

You can put your work back into a queue to avoid a race condition. Alternatively look into lock(). Just make a variable called $lock then you can control access to your output structure. by doing something like this..

my $lock : shared; { lock($lock) ...do something that only 1 thread at a time should do... }

How do I tell which thread is busy and which one is available?

You shouldn't need to. In the scheme below using a queue an idle thread will pick up a unit of work if it can, if there is no more work it will exit and if no work exists yet it will wait.

use threads; use threads::shared; use Thread::Queue; my $max_threads = 8; my $queue = Thread::Queue->new(); print "Spawning threads..."; for(1...$max_threads){ threads->create(\&thread_main)->detach(); } print "Done!\n"; ...Do what you need to do to get a work unit... $queue->enqueue("SOME_WORK_UNIT"); #Signal to the threads they're done. for(1...$max_threads){ $queue->enqueue("DONE"); } wait_on_threads(); exit 0; sub thread_main(){ while(1){ my ($work) = $queue->dequeue()); # If end-of-queue marker reached then cleanup and exit. if($app eq "DONE"){ return 0; } ...do processing work... } } sub wait_on_threads(){ #Dont even start printing messages until our queue is near deplete +d. while($queue->pending() > $max_threads){ sleep 1; } my $cnt = 0; my $wait = 5; while(my $items = $queue->pending() > 0){ # Fortunatly on AIX sleep doesnt seem to put ALL the threads t +o sleep. # whew! This is a non-portable loop though ;) if ($cnt++ > $wait){ print "Please wait. $items items still in progress.\n"; $cnt = 0; $wait++; } sleep 3; } }