一つの配列に結果を追加
use strict;
use warnings;
use IPC::Shareable;
use Parallel::ForkManager;
use Data::Dumper;
### 共有する変数
my $handle = tie( my @result, 'IPC::Shareable', undef, { destroy => 1 } );
### 最大プロセス数
my $pm = Parallel::ForkManager->new(10);
### プロセス起動時に実行するsub routine
$pm->run_on_start(
sub {
my ( $pid, $ident ) = @_;
print "** $ident started, pid: $pid\n";
}
);
for (my $i = 0; $i <= 100; $i++) {
### プロセス起動
$pm->start($i) and next;
### 配列をロック→処理結果を配列に追加→ロックを解除
$handle->shlock;
push( @result, $i );
$handle->shunlock;
### 配列のロックを解除してプロセス終了
$pm->finish($i);
}
### 全てのプロセス終了まで待機
$pm->wait_all_children;
### 結果を確認
print Dumper( \@result );