我想知道异步连接中定时器的一个方面我是否理解正确。
假设我们在执行读取操作之前设置一个计时器,其中包括一个处理程序,然后 run()
io_service。
据我了解,一旦管理器被调用后结束,io_service 就会结束,这可能有两个原因:
a)读取操作完成。
b) 计时器已达到极限。
假设第一个(a)条件已经达到,并且在定时器结束之前读操作已经完成。
问题是:那个计时器会发生什么?我们需要完成它吗? ?
dTimer_.expires_from_now (boost::posix_time::seconds(0));
after the io_service.run()?
如果有必要,您可以将其重置为新的间隔吗?可以将相同的计时器对象重新用于另一个读取操作吗
我可以重置() io_service 并在新操作的新run()
中重用同一对象吗?
There are one aspect of the timers in asynchronous connections I want to know if I understand correctly.
Suppose that we set a timer before performing a read operation, which includes a handler, and then, run()
the io_service.
As I have understood, the io_service ends as soon as the manager ends after being invoked, which can happen for two reasons:
a) the read operation is complete.
b) the timer has reached its limit.
Suppose that the first (a) condition has been reached, and the read operation has been completed before the timer ends.
The question is: What happens to that timer? Do we need to finish it. Say
dTimer_.expires_from_now (boost::posix_time::seconds(0));
after the io_service.run()?
Can you reset it to a new interval if necessary re-use the same timer object for another read operation?
Can I reset() the io_service and reuse the same object in a new run()
for that new operation?
发布评论
评论(1)
如果您不这样做,计时器的处理程序仍然会被调用 取消 它
async_wait 处理程序
将传递一个 boost::asio::error::operation_aborted 的错误代码(如果已成功取消)。如果async_wait
在cancel
之前完成,并且处理程序已由io_service
排队,则您的处理程序将需要检测该条件并做出适当反应。Deadline_timer 可以重置< /a> 使用
expires_from_now
在io_service对象来
run()
或poll()
.org/doc/libs/release/doc/html/boost_asio/reference/io_service/reset.html" rel="nofollow noreferrer">重置它。The timer's handler will still be invoked if you do not cancel it
The
async_wait handler
will be passed an error code ofboost::asio::error::operation_aborted
if it was successfully canceled. If theasync_wait
completed before thecancel
and the handler had already been queued by theio_service
, your handler will need to detect that condition and react appropriately.A deadline_timer can be reset using
expires_from_now
The same
io_service
object can be used again torun()
orpoll()
after resetting it.