perl的多线程

来源:互联网 时间:1970-01-01

注意在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";


相关阅读:
Top