gpt4 book ai didi

c++ - boost asio io_service.run()

转载 作者:IT老高 更新时间:2023-10-28 22:37:22 25 4
gpt4 key购买 nike

我刚刚浏览了 asio chat server example .我的问题是关于他们对 io_service.run() 函数的使用。 io_service.run() 函数的文档说:

The run() function blocks until all work has finished and there are no more handlers to be dispatched, or until the io_service has been stopped. Multiple threads may call the run() function to set up a pool of threads from which the io_service may execute handlers. All threads that are waiting in the pool are equivalent and the io_service may choose any one of them to invoke a handler. The run() function may be safely called again once it has completed only after a call to reset().

它说 run 函数将返回,我假设当它返回时网络线程会停止,直到再次调用它。如果这是真的,那么为什么不在循环中调用 run 函数,或者至少不给它自己的线程? io_service.run() 函数对我来说几乎是个谜。

最佳答案

“直到所有工作都完成并且没有更多的处理程序要分派(dispatch),或者直到 io_service 已停止”

请注意,您确实安装了一个名为 handle_accept 的处理程序,该处理程序会在每次执行时自行重新安装。因此,io_service.run 永远不会返回,至少在您手动退出之前是这样。

基本上,当您在线程中运行 io_service.run 时,io_services proactor 会使用您安装的处理程序接管程序流。从那时起,您将根据事件(如 handle_accept)而不是正常的程序流程来处理程序。您提到的循环位于 asio 的前摄器可怕的深处;-)。

关于c++ - boost asio io_service.run(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1982595/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com