2

私は 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 が機能するかどうかははっきりしていません。

誰でも答えてもらえますか?

ありがとう

4

1 に答える 1

0

正しい疑似コードが次のとおりであると仮定します。

sub timeout
{

$timeout = false;
my $start_time = time();
#$timeout instead of $exit_thread 
while (!$timeout)
{

    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;         
    }
}    
}

次に、いいえ、呼び出しは変数lastを設定しません$timeout-呼び出した時点から単にループを離れます。

これはあなたの質問に答えていますか?

于 2012-10-12T08:18:39.587 に答える