asio::io_context::run_one() with while()
asio::io_context::run_one() with while()
为什么在以下代码中使用asio::io_context::run_one()
时需要while
循环?如果我们不使用while
循环怎么办,如下所示?
size_t numberOfHandler = sock.io_service().run_one();
void set_result(optional<error_code>* a, error_code b)
{
a->reset(b);
}
template <typename MutableBufferSequence>
void read_with_timeout(tcp::socket& sock,
const MutableBufferSequence& buffers)
{
optional<error_code> timer_result;
deadline_timer timer(sock.io_service());
timer.expires_from_now(seconds(1));
timer.async_wait(boost::bind(set_result, &timer_result, _1));
optional<error_code> read_result;
async_read(sock, buffers,
boost::bind(set_result, &read_result, _1));
sock.io_service().reset();
while (sock.io_service().run_one())
{
if (read_result)
timer.cancel();
else if (timer_result)
sock.cancel();
}
if (*read_result)
throw system_error(*read_result);
}
您需要使用 while 循环来处理所有处理程序,以便在超时过后无法在此超时内读取数据时引发异常。
async_wait
的处理程序在 async_read
的处理程序之前执行的情况意味着套接字无法在 1 秒内读取数据。
1. run_one is called
2. handler passed into async_wait is called
3. timer_result is set
4. else if (timer_result) is processed
5. sock.cancel() is called [ cancel() queues handler with error code !!
which indicates that read operation was aborted]
6. handler passed into async_read is queued
传递到async_read
的处理程序已排队,但是何时调用?它只能由 run_one
调用。如果未调用此处理程序,则无法设置read_result
。那么,您希望如何发出信号,以便在一段时间内无法读取数据的情况? read_result
只能通过在 run_one
中调用处理程序来设置。这就是为什么您需要处理内部循环run_one
以调用处理程序,该处理程序read_result
可选,错误代码指示中止的读取操作。
相关文章:
- 为什么"do while"循环不断退出,即使条件计算结果为 false?
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- while循环中while循环的时间复杂度是多少
- Arduino:for/while/if在void setup()或void loop()之前?——错误:之前需要不合格
- 在while循环中输入带有std::cin的字符串后,控制台会输出大量胡言乱语
- 擦除while循环中迭代的元素
- Problems with std::cin.fail()
- 为什么我不能在 while 循环中创建线程?
- 退出简单while循环时出现问题
- 如果条件为TRUE(最佳方式?),则在do while循环中后置增量
- 即使没有满足他们的条件,我也无法通过一些 do-while 循环
- 嵌套While循环不起作用(C++问题)
- 应用程序崩溃并显示"symbol _ZdlPvm, version Qt_5 not defined in file libQt5Core.so.5 with link time reference"
- 如何通过替换顺序代码的while循环来添加OpenMP for循环
- 使用 GMP 的 while 循环出现问题
- "Control reaches end on non-void function" with do { return result; } while(condition);
- asio::io_context::run_one() with while()
- Error with while(cin)
- Use for as while with counter
- Fstream reading by char with while