q987654 发表于 2018-8-31 09:10:29

perl 信号 线程数

  试了一下通过信号来控制并发线程数,测试发现还是不太稳定啊。。各种segment error。。。
  #!/usr/bin/perl -w
  use threads;
  use Thread::Semaphore;
  use Net::Ping;
  my $semaphore = Thread::Semaphore->new(10);
  my $i=1;
  for($i=1;$idown();
  my $thread=threads->create(\&check,$i);
  $thread->detach();
  }
  &waitthread();
  sub check()
  {
  my $num=shift;
  my $host="10.14.12.".$num;
  my $p=Net::Ping->new();
  if($p->ping($host,2))
  {
  print "$host is ok\n";
  }
  else{
  print "$host is not ok\n";
  }
  $p->close();
  $semaphore->up();
  }
  sub waitthread()
  {
  my $tmp=0;
  while($tmp < 10)
  {
  $semaphore->down();
  $tmp++;
  }
  }

页: [1]
查看完整版本: perl 信号 线程数