2011-06-13 3 views

답변

5

활성 스레드를 알리는 신호 처리기를 사용하여 주 스레드에서 alarm을 실행하십시오.

use threads; 
$t1 = threads->create(\&thread_that_might_hang); 
$t2 = threads->create(\&thread_that_might_hang); 
$SIG{ALRM} = sub { 
    if ($t1->is_running) { $t1->kill('ALRM'); } 
    if ($t2->is_running) { $t2->kill('ALRM'); } 
}; 

alarm 60; 
$t1->join; 
$t2->join; 

... 

sub thread_that_might_hang { 
    $SIG{ALRM} = sub { 
     print threads->self->tid(), " got SIGALRM. Good bye.\n"; 
     threads->exit(1); 
    }; 
    ... do something that might hang ... 
} 

당신은 당신이 Alarm::Concurrent처럼 여러 개의 알람을 설정할 수있는 모듈로 보면, 각 스레드에 대해 서로 다른 알람이 필요합니다.

+0

이 솔루션을 사용하면 시스템에서 멈추고 '알람'이 절대로 울리지 않습니다. 'join'은 SIGALRM을 차단하는 것으로 보입니다. 지난 5 년 동안이 새로운 행동입니까? – CDahn