如何使用超时进行异步调用
How to make asynchronous call with timeout
我想在c++中使用timeout进行异步调用,这意味着我想实现类似的目标。
AsynchronousCall(function, time);
if(success)
//call finished succesfully
else
//function was not finished because of timeout
编辑:其中函数是一个方法,需要很多时间,我想打断它,当它需要太多的时间。我一直在寻找如何实现它,我认为boost::asio::deadline_timer
是一条路。我猜呼叫timer.async_wait(boost::bind(&A::fun, this, args))
是我需要的,但我不知道如何查找呼叫是否成功或因超时而中止。
编辑:在永远的答案之后,我的代码现在看起来像这样。
boost::asio::io_service service;
boost::asio::deadline_timer timer(service);
timer.expires_from_now(boost::posix_time::seconds(5));
timer.async_wait(boost::bind(&A::CheckTimer, this, boost::asio::placeholders::error));
boost::thread bt(&A::AsynchronousMethod, this, timer, args); //asynchronous launch
void A::CheckTimer(const boost::system::error_code& error)
{
if (error != boost::asio::error::operation_aborted)
{
cout<<"ok"<<endl;
}
// timer is cancelled.
else
{
cout<<"error"<<endl;
}
}
我想通过引用传递定时器并在异步方法结束时取消它,但是我得到了一个错误,我无法访问在class::boost::asio::basic_io_object中声明的私有成员。
也许使用截止日期计时器不是个好主意?我将非常感谢任何帮助。我将计时器传递给函数,因为调用异步方法的方法本身是异步的,因此我不能为整个类或类似的东西设置一个计时器。
应该使用boost::asio::placeholders::error
timer.async_wait(boost::bind(
&A::fun, this, boost::asio::placeholders::error));
A::fun(const boost::system::error_code& error)
{
// timeout, or some other shit happens
if (error != boost::asio::error::operation_aborted)
{
}
// timer is cancelled.
else
{
}
}
相关文章:
- 如何在 c++ 中异步调用静态方法?
- 在 NodeJS 异步调用C++ DLL
- Qt异步调用:如何在异步调用完成任务后运行一些东西
- 重构后,异步调用方法不再有效
- 通过谷歌测试测试异步调用
- 如何使用从Swift代码中调用的线程在C 上制作异步调用功能
- 如何从 dart 对用 C/C++ 编写的函数进行异步调用
- 异步调用槽,而不使用清晰的代码行连接到它
- 修复此标准::异步调用
- V8 JavaScript 如何进行异步调用
- 进行异步调用 - 我必须创建自己的p/s吗?
- C++异步调用最佳实践
- 处理多个std::异步调用
- JNI:C++是否异步调用Java
- 使用 Emscripten 异步调用 JavaScript C++函数
- 如何在 Kernal32.dll 中异步调用 ReadFile()?C#.
- 使用JNI异步调用Java方法
- C++:如何异步调用同步库调用
- 对ReadFile函数的异步调用返回6个错误代码
- 在c++中异步调用CreateFile和ReadFile