foreach $name (@project_list)
{
# a thread is created for each project
my $t = threads->new(\&do_work, $name);
push(@threads, $t);
}
foreach (@threads) {
my $thrd = $_->join;
print "Thread $thrd done\n";
}
sub do_work {
# execute some commands here...
}
The project_list is a list of 40 items. When I spawn a thread for each item, will the join method wait for the first thread to finish and then move over to the next one and so on?
If that is the case, then is it possible to avoid it? I mean some threads will finish faster then others so why wait?
Please let me know if more information is required.
Thank you.
$_->joinwaits for the thread designated by$_to finish. Since you’re pushing them in order, andforeachtraverses the list in order, yes, you’ll wait first for the first thread.But that doesn’t matter since you’re waiting for all threads to finish. It doesn’t matter if you wait for the fastest finishers or the slowest ones first – you’ll be waiting for everyone anyway.