如何使用升压执行监视器,以确保如果我的功能得到雄,它将在30分钟之后超时?(How do I use

2019-10-17 10:57发布

可有人请使用Boost执行监控告诉我一个例子代码? 这引起了对红帽运行(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


}

Answer 1:

要做到这一点,你只需要:

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

这意味着,预计该函数的签名来调用返回和诠释,不采取任何参数。 如果你的函数不匹配签名所需使用的boost ::绑定或手卷函数包装。

完整的例子:

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


Answer 2:

你不一定需要升压做到这一点。 实现超时一个非常简单和常用的技术是一个看门狗定时器 。 从本质上讲,“在检查”与定期经理线程的工作线程或进程。 这可以简单地通过设置变量来实现(尽管它应该使用类似的新的C ++ 11原子库原子公司,或者与互斥锁保护)。 如果在规定时间内的工人还没有签入,你可以杀死工人,或任何你选择的方式处理错误。



文章来源: How do I use Boost execution monitor to make sure if my function get hung, it will timeout after 30 mins?
标签: c++ boost