perl的多线程

注意在perl5.8中,必须用threads->create来生成线程,和许多介绍旧版perl的书籍中的有区别

use Thread qw/async yield/;
my $done=0;
sub frob {
 my $arg=shift;
 my $tid=Thread->self->tid;
 print "thread $tid:frob $arg\n";
 yield;
 unless ($done) {
  yield;
  $done++;
  frob($arg+10);
 }
}

my @t;
for my $i (1..3) {
 push @t,threads->create(\&frob,$i);
}
for (@t) {$_->join}
print "done is $done\n";

你可能感兴趣的:(perl)