我正在玩这段代码,从哔哔声的方式来看,一旦程序启动它就不会停止。我怀疑问题出在$thread->kill('KILL')->detach;
.
有任何想法吗?还是我做错了什么?
#!c:/perl/bin/perl
use threads;
use Audio::Beep;
my $thread;
my $playing=0;
while (1) {
my $val = int( rand(10) );
print "$val\n";
# if event "start" occurs and is not playing create thread
#if ( ($val==0 || $val==1 || $val==2 || $val==3 || $val==4 ) && (!$playing) ) {
if ( ($val==0 || $val==1 || $val==2 ) && (!$playing) ) {
$playing = 1;
$thread = threads->create(
sub {
local $SIG{'KILL'} = sub { threads->exit() };
print "start beep\n";
beep( 520, 10000 );
}
);
}
# if event "end" occurs and playing wait 1 seconf and kill thread
elsif ( ($val==5 ) && $playing ) {
print "stop beep \n";
#sleep(1);
$playing = 0;
$thread->kill('KILL')->detach;
}
#sleep(1);
$count = 0;
for($i=0; $i<99999 ; $i++) {
$count++;
}
}