私は perl を初めて使用し、perl スレッドについて質問があります。
実行中の関数がタイムアウトしているかどうかを確認するために新しいスレッドを作成しようとしています。その方法は次のとおりです。
ロジックは 1.新しいスレッドを作成する 2.メイン関数を実行し、タイムアウトしているかどうかを確認し、タイムアウトした場合は強制終了します
サンプルコード:
$exit_tread = false; # a flag to make sure timeout thread will run
my $thr_timeout = threads->new( \&timeout );
execute main function here;
$exit_thread = true # set the flag to true to force thread ends
$thr_timeout->join(); #wait for the timeout thread ends
タイムアウト関数のコード
sub timeout
{
$timeout = false;
my $start_time = time();
while (!$exit_thread)
{
sleep(1);
last if (main function is executed);
if (time() - $start_time >= configured time )
{
logmsg "process is killed as request timed out";
_kill_remote_process();
$timeout = true;
last;
}
}
}
現在、コードは期待どおりに実行されていますが、 while ループの最後に「最後」があるため、コード $exit_thread = true が機能するかどうかははっきりしていません。
誰でも答えてもらえますか?
ありがとう