in reply to Thread exit errors

Is there a way to stop the first thread from exiting until all the other threads are finsished?
In the main thread, do something like:
$_->join for threads->list;
although that could interfere with the threads being created in runSeries (I don't understand why you create a new thread there), in which case do this instead:
my @threads; foreach $host (@syshost) { push @threads, threads->new(\&runSeries, $host); } $_->join for @threads;

Dave.

Replies are listed 'Best First'.
Re^2: Thread exit errors
by JFarr (Sexton) on Nov 17, 2005 at 22:52 UTC
    Thanks Dave. I'm still very noobish to Perl, so I have a question on your response. the $_->join for threads->list, what does this do. The problem we are having, is that are services have a delay in their response time, being that our farthest host is between 200-400 miles from our distribution. Is this command going to keep the threads alive until a response if found? The reason for the multi threads is that the main thread, launches a method that is launching other threads to search for all of the servies on each host. So,
    foreach $host(@syshosts) { @sysThread = threads->new( \&findHostNodes, $host ); } sub findHostNodes { my $host = @_; #print some stuff about the node #now find the services on the host foreach $service (@r21services) { @serviceThread = threads->new(\@shutdonwServices, $service); } } sub shutdownServices { shut down the windows services we are looking for }
    From you response, in your opinion, what is the better method? Thanks, Jeff
      $_->join for threads->list, what does this do.

      It simply waits for, and then joins, any remaining threads.

      Your problem is simply to ensure that the main process doesn't exit until all threads have been joined. My example is just a simple way to ensure that all remaining threads have been joined.

      Please bear in mind that threads in perl are extremely expensive to create and consume a lot of memory, so I suspect that creating new threads in findHostNodes is a bad idea.

      Dave.