在下面的子程序中(造成错误的代码的简化版本),每个线程都应该添加到主输出列表。虽然在子程序中,数组似乎正在填充,但当我回到主调用部分时,它又变为空了。我做错了什么吗?
#!/usr/bin/env perl
use threads;
use strict;
my $num_threads = 8;
my @threads = initThreads();
our @outputArray;
foreach(@threads){
$_ = threads->create(\&do_search);
}
foreach(@threads){
$_->join();
}
print "@outputArray";
sub initThreads{
# An array to place our threads in
my @initThreads;
for(my $i = 1;$i<=$num_threads+1;$i++){
push(@initThreads,$i);
}
return @initThreads;
}
sub do_search{
my $id = threads->tid();
push(@outputArray,$id);
threads->exit();
}