C++ 使用boost线程库引发异常
我有以下代码来运行定时线程:C++ 使用boost线程库引发异常,c++,boost,boost-thread,C++,Boost,Boost Thread,我有以下代码来运行定时线程: // Method to invoke a request with a timeout. bool devices::server::CDeviceServer::invokeWithTimeout(CDeviceClientRequest& request, CDeviceServerResponse& response) { /
// 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);
}
但这也会引发异常并使应用程序崩溃。我需要一个完全安全的机制
如果已达到超时,则可以安全终止定时线程。Boost文档说明: 如果传递给boost::thread构造函数的函数或可调用对象在调用时传播了一个非boost::thread_中断类型的异常,则调用std::terminate() 必须捕获异常并干净地退出线程(或抛出boost::thread\u interrupted)
好的,我提出的解决方案代码是:
// 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);
}
//在定时线程内帮助调用设备方法的Functor。
结构调用
{
void operator()(设备::服务器::CDeviceServer和服务器,
boost::promise&boostPromise,
CDeviceClient请求和请求,
CDeviceServerResponse和response)
{
尝试
{
调用(请求、响应);
boostPromise.set_value();
}
捕获(设备::util::CDeviceException&e)
{
//在响应中添加任何错误。
std::string message=devices::util::retrieveDeviceExceptionMessage(e);
响应。设置错误消息(消息);
}
捕获(标准::异常&e)
{
//向响应中添加任何异常消息。
字符串消息(例如what());
响应。设置错误消息(消息);
}
}
};
//方法调用具有超时的请求。
bool devices::server::CDeviceServer::invokeWithTimeout(CDeviceClientRequest&request,
CDeviceServerResponse和response)
{
//从设备检索超时。
int timeout=getTimeout();
timeout+=500;//添加500毫秒以覆盖调用时间。
//在定时线程中调用请求。
承诺,承诺,承诺;
boost::unique_future boostFuture=boostPromise.get_future();
boost::thread boostThread([&]()
{
调用函数;
函子(*这个,
boostPromise,
要求
反应);
});
//如果未来尚未准备就绪,线程已超时。
返回(boostFuture.wait_for(boost::chrono::毫秒(超时))
==
boost::future_status::ready);
}
在上述两个示例中,在哪里捕捉线程?在boostThread运行的代码内部,在返回或中断()附近?异常抛出的确切位置,我需要在哪里捕获它?同时查看文档,可以简单地分离()线程吗?
// 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);
}