0

我有一个多线程的主 Perl 脚本。我想要做的是每个线程将运行 3 个命令,一旦整个线程完成,我希望我的脚本再次触发同一个线程,基本上是在一个循环中,谁能告诉我该怎么做

这是我的perl 代码片段

my @finalOneClickarray= grep(/\S/, @oneclickConcurrentbackup); #----Removing the empty content from the array---#   

#---Making Concurrent OneClick backup commands using threads------#
my @threads;

#-----performing the CHO loops as given by user-------#
foreach (@finalOneClickarray) {
    push @threads, threads->new(\&concurrentBackupCommandsRead, $_);
}

foreach (@threads) {
    $_->join();
}

concurrentBackupCommandsRead是执行命令的方法。

更新的 perl 代码:-

use threads;
use threads::shared;
my @arr = (1,2,3,4);
my $outnumber :shared =4;


print "\n variable outside thread that is in main program $outnumber\n";
my @threads;


for($i=0;$i<=3; $i++)
{
  print "\ncalling subrountine vinay for $i times\n";

  vinay();

}

sub vinay()
{
    foreach (@arr) {
       push @threads, threads->create(\&doSomething);
    }
    foreach (@threads) {
       $_->join();
    }
}

sub doSomething ()
{

 print "\n Before increment is $outnumber\n";
 my $foo = $outnumber; 
 $outnumber = $foo + 1;

print "\n After increment is $outnumber\n";

}
4

1 回答 1

1

在您第二次运行循环时,您正在尝试加入一个已经加入的线程(因为您只添加到@threads)。这会引发错误,“线程已加入”。

清除@threads,或将其本地化为vinay(),例如。

sub vinay()
{
    my @threads;
    foreach (@arr) {
       push @threads, threads->create(\&doSomething);
    }
    foreach (@threads) {
       $_->join();
    }
}
于 2013-09-15T14:37:35.097 回答