0

我有以下代码来运行定时线程:

// Method to invoke a request with a timeout.
bool devices::server::CDeviceServer::invokeWithTimeout(CDeviceClientRequest&  request,
                                                       CDeviceServerResponse& response)
{
   // Retrieve the timeout from the device.
   int timeout = getTimeout();
   timeout += 500; // Add 500ms to cover invocation time.

   // Invoke the request within a timed thread.
   boost::promise<void>        boostPromise;
   boost::unique_future<void>  boostFuture = boostPromise.get_future();
   boost::thread               boostThread([&]()
                               {
                                  invoke(request, response); 
                                  boostPromise.set_value();
                               });

   // The thread has timed out, if the future is not ready.
   return (boostFuture.wait_for(boost::chrono::milliseconds(timeout)) 
           == 
           boost::future_status::ready);
}

这似乎没有问题,该函数在超时时返回 false。

然而,被调用的代码(通过invoke(request, response);)会抛出一个异常,从而杀死应用程序。如果线程尚未完成,我如何成功终止线程并处理任何异常。

我尝试了以下方法:

// The thread has timed out, if the future is not ready.
bool completed = (boostFuture.wait_for(boost::chrono::milliseconds(timeout)) 
                  == 
                  boost::future_status::ready);
if (!completed)
{
   boostThread.interrupt();
}
return completed;

但这也会引发异常并使应用程序崩溃。我需要一个完全安全的机制,如果达到超时,可以安全地关闭定时线程。

4

2 回答 2

1

Boost 文档指出:

如果传递给 boost::thread 构造函数的函数或可调用对象在调用时传播非 boost::thread_interrupted 类型的异常,则调用 std::terminate()。

您必须捕获异常并干净地退出线程(或抛出 boost::thread_interrupted)

http://www.boost.org/doc/libs/1_54_0/doc/html/thread/thread_management.html#thread.thread_management.tutorial.exceptions

于 2013-08-12T17:58:55.687 回答
0

好的,我想出的解决方案的代码是:

// Functor to help invoke the device method, inside a timed thread.
struct invoke_fn
{
   void operator()(devices::server::CDeviceServer& server,
                   boost::promise<void>&           boostPromise,
                   CDeviceClientRequest&           request,
                   CDeviceServerResponse&          response)
   {
      try
      {
         server.invoke(request, response);
         boostPromise.set_value();
      }
      catch (devices::util::CDeviceException &e)
      {
         // Add any error to the response.
         std::string message = devices::util::retrieveDeviceExceptionMessage(e);
         response.set_errormessage(message);
      }
      catch (std::exception &e)
      {
         // Add any exception message to the response.
         std::string message(e.what());
         response.set_errormessage(message);
      }
   }
};

// Method to invoke a request with a timeout.
bool devices::server::CDeviceServer::invokeWithTimeout(CDeviceClientRequest&  request,
                                                       CDeviceServerResponse& response)
{
   // Retrieve the timeout from the device.
   int timeout = getTimeout();
   timeout += 500; // Add 500ms to cover invocation time.

   // Invoke the request within a timed thread.
   boost::promise<void>        boostPromise;
   boost::unique_future<void>  boostFuture = boostPromise.get_future();
   boost::thread               boostThread([&]()
                               {
                                  invoke_fn functor;
                                  functor(*this,
                                          boostPromise,
                                          request,
                                          response);
                               });

   // The thread has timed out, if the future is not ready.
   return (boostFuture.wait_for(boost::chrono::milliseconds(timeout)) 
           == 
           boost::future_status::ready);
}
于 2013-08-13T12:42:28.120 回答