Код | use threads 'exit' => 'threads_only';
my @ths; my $max = 10; $| = 1;
foreach my $num (1..$max) { push @ths, threads->create( sub { my $num = shift; $SIG{'KILL'} = sub { print "catch kill for thread $num\n"; threads->exit; }; print "thread $num started\n"; while(1) { if ($num % 2 == 0) { eval { my $line = readline(STDIN); }; } else { sleep 1; } } }, $num ); }
sleep 3;
foreach my $num (1..$max) { print "send kill to $num\n"; $ths[$num-1]->kill('KILL'); }
print "end, wait 3 sec\n\n";
sleep 3;
|
такой вывод получается:
Код | thread 1 started thread 2 started thread 3 started thread 4 started thread 5 started thread 6 started thread 7 started thread 8 started thread 9 started thread 10 started send kill to 1 send kill to 2 send kill to 3 send kill to 4 send kill to 5 send kill to 6 send kill to 7 send kill to 8 send kill to 9 send kill to 10 end, wait 3 sec
catch kill for thread 7 catch kill for thread 9 catch kill for thread 1 catch kill for thread 3 catch kill for thread 5 Perl exited with active threads: 5 running and unjoined 5 finished and unjoined 0 running and detached
|
как завершать такие потоки с подвисшим ожиданием данных в eval? |