1

Boost実行モニターを使用したサンプルコードを誰かに見せてもらえますか? これは Redhat (6.0 ??) で実行する必要があります

int main(int argc, char *argv[])
{
   runTest();//I need a timeout of 30 mins here. That way if the gets hung, process will get killed


}
4

2 に答える 2

3

これを実現するには、次のことを行う必要があります。

  1. を使用して「unit_test_monitor」のハンドルを取得しますauto& inst = boost::test::unit_test_monitor::instance()
  2. を使用して目的のタイムアウト値を設定しますinst.p_timeout.set( num_seconds );
  3. 実行モニターのexecute()メソッドを介して関数を呼び出します。

実行モニターのexecute()メソッドには、次のシグネチャがあります。

int execute( unit_test::callback0<int> const& F ); 

これは、関数のシグネチャがreturnとintを呼び出し、引数をとらないことを期待していることを意味します。関数が必要な署名と一致しない場合は、boost::bindまたは手動でロールされた関数ラッパーを使用してください。

完全な例:

#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE MyTest
#include <boost/test/included/unit_test.hpp>
#include <boost/test/unit_test_monitor.hpp>
#include <boost/bind.hpp>

#include <unistd.h>

namespace bt=boost::unit_test;


bool DodgyFunc( unsigned wait )
{
    for( unsigned x=0; x<wait; ++x)
    {
        std::cout << "Sleeping....\n";
        usleep( 1000000 );
    }

    return true;
}

BOOST_AUTO_TEST_CASE(MyTestCase)
{
    // simple call
    std::cout << "Simple call to DodgyFunc, this will pass\n";
    BOOST_CHECK_EQUAL( true, DodgyFunc( 5 ) );

    // get the execution monitor instance
    bt::unit_test_monitor_t& theMonitor = bt::unit_test_monitor_t::instance();

    // Set the  timeout
    theMonitor.p_timeout.set( 3 );

    // Now call using the execution monitor
    std::cout << "\n\nCall to DodgyFunc, using the execution monitor " 
              << "this will timeout and result in an error\n";
    theMonitor.execute( boost::bind( DodgyFunc, 10 ) );
}
于 2012-12-04T08:05:58.110 に答える
1

これを行うために必ずしも Boost が必要なわけではありません。タイムアウトを実装するための非常に単純で一般的な手法は、ウォッチドッグ タイマーです。基本的に、ワーカー スレッドまたはプロセスは、一定の間隔でマネージャー スレッドに「チェックイン」します。これは、変数を設定するだけで簡単に実行できます (ただし、新しい C++11 アトミック ライブラリのようなものを使用したアトミックであるか、ミューテックスで保護されている必要があります)。ワーカーが割り当てられた時間内にチェックインしなかった場合は、ワーカーを強制終了するか、任意の方法でエラーを処理できます。

于 2012-12-03T20:00:45.473 に答える