问题描述
io_service::reset
文档指出必须先调用reset()
,然后再调用run()
,run_one()
,poll()
或poll_one()
.
io_service::reset
documentation states that reset()
must be called before subsequent calls to run()
, run_one()
, poll()
or poll_one()
.
问题:
- 为什么这是必要的? -
- 如果忽略此步骤,我会期望什么行为?
- 为什么这个要求不够重要,以致不能忽略
assert
?
一些上下文:我完成了一些单元测试的调试,这些单元测试反复检查了调用poll()
的单元,而没有reset()
并试图检查每次执行的处理程序的预期数量.看来,对poll()
的调用足够多,最终所有处理程序都将按预期的顺序执行,但是它需要的调用比您期望的要多.正确调用reset()
可以解决此问题,但我很想知道这是否是不调用reset()
的唯一副作用,或者是否存在潜在的更糟糕的影响,例如删除处理程序或可能在多线程示例.
Some context: I finished debugging some unit-tests that checked that called poll()
repeatedly without reset()
and was attempting to check the expected number of handlers was being executed each time. It appears that with enough calls to poll()
, all handlers are eventually executed in the order expected, but it takes more calls than you would otherwise expect. Correctly calling reset()
fixes the problem, but I'm curious to know if this is the only side effect of not calling reset()
, or if there are potentially worse effects such as dropping handlers or effects that might appear in a multi-threaded example.
推荐答案
io_service
停止后:
-
poll()
,poll_one()
,run()
和run_one()
的所有调用将尽快返回 - 随后对
poll()
,poll_one()
,run()
和run_one()
的调用将立即返回,而无需调用任何处理程序或处理事件循环
- all invocations of
poll()
,poll_one()
,run()
, andrun_one()
will return as soon as possible - subsequent calls to
poll()
,poll_one()
,run()
, andrun_one()
will return immediately without invoking any handlers or processing the event loop
调用 io_service::reset()
将io_service
设置为不再处于停止状态,从而允许随后对poll()
,poll_one()
,run()
和run_one()
的调用来调用处理程序并处理事件循环.
Invoking io_service::reset()
sets the io_service
to no longer be in a stopped state, allowing subsequent calls to poll()
, poll_one()
, run()
, and run_one()
to invoke handlers and process the event loop.
如果通过 io_service.stop()
或隐瞒工作时间.
It is necessary if one wishes to invoke handlers or process the event loop once the io_service
has been stopped explicitly via io_service.stop()
or implicitly by running out of work.
如果io_service.stopped()
是true
,则随后对poll()
,poll_one()
,run()
和run_one()
的调用将不会执行任何工作.
If io_service.stopped()
is true
, then subsequent calls to poll()
, poll_one()
, run()
, and run_one()
will not perform any work.
io_service::reset()
文档中对必须"一词的使用往往会设置过于挑剔的语气,而没有提及不调用reset()
的后果. io_service::stop()
不足以保证错误:
The io_service::reset()
documentation's use of the word "must" tends to set an overly critical tone without mentioning the consequences of not calling reset()
. The behavior described by io_service::stop()
is not critical enough to warrant an error:
对于reset()
,唯一的硬性要求是在未完成对poll()
,poll_one()
,run()
和run_one()
的调用时不调用它.
For reset()
, the only hard requirement is to not call it when there are unfinished calls to poll()
, poll_one()
, run()
, and run_one()
.
这篇关于为什么必须调用io_service :: reset()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!