2015-03-31 107 views
0
sub handle_sigterm { 
    my @running = threads->list(threads::running); 
    for my $thr (@running) { 
     $thr->kill('SIGTERM')->join(); 
    } 
    threads->exit; 
} ## end sub handle_sigterm 


OUTPUT: 
Perl exited with active threads: 
     1 running and unjoined 
     0 finished and unjoined 
     1 running and detached 

看起來像handle_sigterm退出時沒有清理線程?Perl:退出前清理活動線程

我能做些什麼清理線程?

回答

2

threads->exit不會做你認爲的事情。它退出當前線程,不是所有線程。在線程之外,就像調用exit一樣。

threads->exit() 
    If needed, a thread can be exited at any time by calling 
    "threads->exit()". This will cause the thread to return "undef" in 
    a scalar context, or the empty list in a list context. 

    When called from the main thread, this behaves the same as exit(0). 

你想要的是要麼等待所有線程完成...

$_->join for threads->list; 

或者脫離所有的線程,他們將在程序退出時終止。

$_->detach for threads->list; 

此外,要使用threads->list獲得所有非固定,非分離線程的列表,運行與否。 threads->list(threads::running)只會給你仍在運行的線程。如果任何線程已完成但尚未加入,則將被錯過。