![](/img/trans.png)
[英]How to make an asynchronous call from dart the function written in C/C ++?
[英]How to make asynchronous call with timeout
我想在C ++中使用超时进行异步调用,这意味着我想要实现这样的功能。
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))
是我所需要的,但是我不知道如何找到呼叫是成功还是由于超时而中止。
编辑:从ForEveR的答案后,我的代码现在看起来像这样。
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;
}
}
我想通过引用传递计时器,并在异步方法的最后将其取消,但是出现一个错误,我无法访问在:: 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
{
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.