2011-11-25 86 views
0

我有一个数组,它载有我将要创建的文件名。我编写了下面的代码,一次创建一个文件。如何使用perl为子例程创建多个线程?

use strict; 
use File::Slurp; 

my @files_to_create=(file_1,file_2......file_100000); 
my $File_Con="blah blah..."; 

foreach my $create_file(@files_to_create){ 
    &Make_File($create_file); 
} 

sub create_file{ 
    my $to_make=shift; 
    write_file($to_make,$File_Con); 
} 

我想和大家分享多标量的子过程中的数组..因此,我可以减少文件的创建时间..任何人都可以建议步骤做...?

回答

1

有关如何在Perl中使用线程的非常好的教程,请参阅perldoc perlthrtut

use strict; 
use warnings; 
use threads; 

sub create { ... } 

my @files_to_create = map { "file_$_" } 1 .. 100_000; 
my $config = "blah blah"; 

my @threads; # To store the threads created 

foreach my $file (@files_to_create) { # Create a thread for each file 

    my $thr = threads->new(\&create, $file, $config); 
    push @threads, $thr; 
} 

$_->join for @threads; # Waits for all threads to complete 
相关问题