我是 perl 新手,在我的 perl 脚本中处理 ^C 时遇到问题。当我在睡眠期间收到 ^C 后尝试继续执行脚本时,我只在 $FLAG = 2; 之前输出 之后什么都没有:
# perl test.pl
sleeping...
^Cawaiking... =
#
代替:
# perl test.pl
sleeping...
awaiking... ====
some..
#
它接缝 ^C 正在杀死进度条线程,并且在它死后没有任何动作,但可以在主线程中执行打印。谁能帮我解决这个问题?
$SIG{INT} = 'IGNORE';
our $FLAG : shared = 1;
...
sub call1{
$FLAG = 1;
my $pBar = threads->new(\&progressBarInit);
$pBar->detach;
print "sleeping...\n";
sleep 5;
print "awaiking...\n";
$FLAG = 2;
print "some..\n";
return @result;
}
call1();
sub progressBarInit{
my $max = 50;
my $counter = 1;
while($FLAG == 1){
progressBar( $counter, $max, 50, '=' );
$counter++;
if($counter > $max){$counter=1;}
sleep 1;
}
}
sub progressBar {
my ( $counter, $max, $width, $char ) = @_;
local $| = 1;
printf " %-${width}s\r", $char x (($width-1)*$counter/$max);
}