在Perl (>=5.012) 线程中,有没有办法实现闹钟(或其他超时机制)?
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;
sleep 1 until $t1->is_joinable; $t1->join;
sleep 1 until $t2->is_joinable; $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
。
编辑:评论者指出threads::join
会干扰SIGALRM
,因此您可能需要测试$thr->is_joinable
而不是调用$thr->join
alarm
从未触发。看起来join
阻塞了 SIGALRM。这是过去五年中的新行为吗? - CDahnthr->join()
,但是没有,所以现在我打算用thr->is_joinable()
来轮询线程状态,以免阻塞SIGALRM。 - solstice333