我的代码如下所示:
use threads;
use threads::shared;
use Thread::Queue;
my $q = Thread::Queue->new();
my @threads = ();
my $run :shared = 1;
$SIG{'TERM'} = sub {
$run = 0;
$q->enqueue('exit');
foreach(@threads){
$_->join();
}
};
push @threads, threads->create(proc1);
push @threads, threads->create(proc2);
sub proc1 {
while($p = $q->dequeue()){
if($p eq 'exit'){
last;
}
.....
}
$q->end();
threads->exit();
}
sub proc2 {
while($run){
.....
}
}
收到 TERM 信号时,我尝试等待所有线程结束。然而,每当我传递 TERM 信号时,我的程序都会因错误而终止。
Segmentation fault
如何修复这个问题?