Reputation: 1779
I have a multi-thread perl script using threads
, works ok but when it tries to kill the hanged threads I got an error and script exists:
for (0..$threads-1) {
$trl[$_] = threads->create(\&my_sub, $_);
$SIG{ALRM} = sub {
if ($trl->is_running) {
$trl->kill('ALRM');
}
}
}
for (@trl) {
alarm 10;
$_->join;
}
and
sub my_sub {
$SIG{ALRM} = sub {
threads->exit(1);
};
while (@site) {
{
lock(@site);
$url = shift @site;
}
and the error:
Can't call method "is_running" on an undefined value at test.pl line 61.
Perl exited with active threads:
9 running and unjoined
40 finished and unjoined
0 running and detached
the line 61 is:
if ($trl->is_running) {
Upvotes: 0
Views: 282
Reputation: 53478
OK, please turn on strict
and warnings
. This would tell you that @trl
and $trl
are not the same thing. Specifically - your 'signal handler' in your main program - you're redefining every time you start a thread, and so it'll only kill the latest one. (Except it won't, because $tr1
is undefined).
I would suggest you don't want to mix signals and threads anyway though - this depends rather more on what you're doing. But usually I'd suggest - fork
and kill
. thread
and Thread::Semaphore
or Thread::Queue
.
Upvotes: 4