提高ASIO io_service.run() [英] boost asio io_service.run()

查看:579
本文介绍了提高ASIO io_service.run()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正要过ASIO 聊天服务器的例子。我的问题是关于他们的 io_service.run()函数的用法。为 io_service.run()函数的文档说:

I was just going over the asio chat server example. My question is about their usage of the io_service.run() function. The documentation for the io_service.run() function says:

的run()函数阻塞,直到所有的工作已经完成,也没有
  更多的处理程序被分派,或直至io_service对象已
  停止。多个线程可以调用run()函数来设置一个
  从中io_service对象可以执行处理程序的线程池。所有
  正在等待的线程池是等同的。该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函数将返回,和我假设当它返回的网线停止,直到再次调用。如果这是真的,那么为什么不叫在一个循环中运行功能,或至少给自己的线程?在 io_service.run()的功能是pretty多一个谜给我。

It says that the run function will return, and I'm assuming that when it does return the network thread stops until it is called again. If that is true, then why isn't the run function called in a loop, or at least given its own thread? the io_service.run() function is pretty much a mystery to me.

推荐答案

,直到所有的工作已经完成,并且没有更多的处理程序进行调度,或者直到io_service对象已停止

"until all work has finished and there are no more handlers to be dispatched, or until the io_service has been stopped"

这是你的的通知DO 安装的处理程序,名为 handle_accept ,在每个执行重新安装本身。因此, io_service.run 将永远不会返回,至少直到手动退出它。

Notice that you DO install a handler, named handle_accept, that reinstalls itself at each execution. Hence, the io_service.run will never return, at least until you quit it manually.

基本上,你在一个线程中运行io_service.run的那一刻,io_services摄接管程序流程,使用安装了处理程序的。从这一点上,您处理基于事件的程序(如 handle_accept ),而不是正常的程序的程序流程。你提的循环是在ASIO的摄的吓人深处的某个地方深; - )

Basically, at the moment you run io_service.run in a thread, io_services proactor takes over program flow, using the handler's you installed. From that point on, you handle the program based on events (like the handle_accept) instead of normal procedural program flow. The loop you're mentioning is somewhere deep in the scary depths of the asio's proactor ;-).

这篇关于提高ASIO io_service.run()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆