需要对 Boost asio 异步操作和计时器进行一些澄清

发布于 2024-10-19 08:01:08 字数 503 浏览 4 评论 0 原文

我想知道异步连接中定时器的一个方面我是否理解正确。

假设我们在执行读取操作之前设置一个计时器,其中包括一个处理程序,然后 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

还不是爱你 2024-10-26 08:01:08

问题是:那会发生什么
定时器?我们需要完成它吗?

如果您不这样做,计时器的处理程序仍然会被调用 取消

void my_read_handler() {
     dTimer_.cancel(); // remember to catch exceptions
}

async_wait 处理程序 将传递一个 boost::asio::error::operation_aborted 的错误代码(如果已成功取消)。如果 async_waitcancel 之前完成,并且处理程序已由 io_service 排队,则您的处理程序将需要检测该条件并做出适当反应。


如果出现以下情况,您可以将其重置为新的间隔吗
必要时重复使用相同的计时器对象
进行另一个读取操作?

Deadline_timer 可以重置< /a> 使用 expires_from_now

该函数设置到期时间。
任何挂起的异步等待
操作将被取消。这
每个取消操作的处理程序
将被调用
升压::asio::错误::操作中止
错误代码。


我可以重置() io_service 并重用吗
新的 run() 中的同一对象
那个新操作?

io_service对象来run()poll() .org/doc/libs/release/doc/html/boost_asio/reference/io_service/reset.html" rel="nofollow noreferrer">重置它。

该函数必须在调用之前调用
任何第二组或以后的调用集
run()、run_one()、poll() 或
poll_one() 函数当前一个
调用这些函数返回
由于 io_service 被停止或
没工作了。这个功能
允许 io_service 重置任何
内部状态,例如“停止”
标志。

The question is: What happens to that
timer? Do we need to finish it.

The timer's handler will still be invoked if you do not cancel it

void my_read_handler() {
     dTimer_.cancel(); // remember to catch exceptions
}

The async_wait handler will be passed an error code of boost::asio::error::operation_aborted if it was successfully canceled. If the async_wait completed before the cancel and the handler had already been queued by the io_service, your handler will need to detect that condition and react appropriately.


Can you reset it to a new interval if
necessary re-use the same timer object
for another read operation?

A deadline_timer can be reset using expires_from_now

This function sets the expiry time.
Any pending asynchronous wait
operations will be cancelled. The
handler for each cancelled operation
will be invoked with the
boost::asio::error::operation_aborted
error code.


Can I reset() the io_service and reuse
the same object in a new run() for
that new operation?

The same io_service object can be used again to run() or poll() after resetting it.

This function must be called prior to
any second or later set of invocations
of the run(), run_one(), poll() or
poll_one() functions when a previous
invocation of these functions returned
due to the io_service being stopped or
running out of work. This function
allows the io_service to reset any
internal state, such as a "stopped"
flag.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文