- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我刚刚浏览了 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/
我有一个 ip::udp::socket用 io_service 构建.只有一个 boost::thread调用 io_service::run()方法,以及 io_service::work 的一个
我为异步 io_service 工作运行 aync 作业线程。 我想销毁用于异步作业的资源。 boost::asio::io_service boost::asio::io_service::work
我有一个 boost::asio 线程池,它有一个 asio::io_service和 asio::work : boost::asio::io_service m_service; boost::a
我正在编写多线程 TCP 服务器,基于应用程序设计,我需要有多个线程,每个线程都有 io_service。 通过该设计,我需要接受来自一个Thread/io_service 的连接,进行身份验证过程(
如果我io_service::run()只在一个线程中运行,那么io_service::post()调用是在我要求它们执行的顺序相同,或者它们可以按任意顺序执行,我仍然需要使用 strand 来强制序
boost::asio::io_service m_io_service; boost::asio::io_service::work m_work(m_io_service); m_io_servi
长话短说,我的代码: #include #include #include #include #include #include namespace ba = boost::asio; n
我的简化问题 我阅读了this thread,并试图删除io_service对象。我做这个 m_IO.stop(); m_IO.~io_service(); m_IO是boost::asio::io_
我使用 boost::asio::io_service 来处理 Foo 中的工作。现在它工作正常,直到我退出 ~Foo 并卡在 io_service 析构函数(在 service_registry 的
我想在里面的类中周期性地制作一个10秒的计时器,但是它不起作用。它会在 10 秒内第一次打印计数。但在那之后,它不会再等待 10 秒。具体代码。 #include #include #includ
我正在尝试运行 udp 服务器。问题是阻塞了 io_service 上的 run() 调用。所以我决定使用 boost bind 在其他线程上运行这个方法。结果主线程执行超出了 DictionaryI
我一直在阅读 boost asio 文档,特别是关于 Proactor 设计的。 当我看到这个标题的时候 前摄器设计模式:无线程的并发 我的理解是,asio 的用户不需要创建额外的线程来执行异步 IO
我想让程序等到它完成所有正在运行的线程,这与 ioService.stop(); 不同,后者无需等待即可停止 ioService。我尝试了以下代码,它工作正常,但没有等待线程完成就停止了 ioServ
为什么在这个简单的类中,如果我直接使用 io.run() 函数将被调用,否则如果要求运行到其他线程,打印将不会被调用? #include #include #include using name
正在尝试学习 asio,我正在关注网站上的示例。 为什么需要 io_service 以及它的具体作用是什么?为什么我在执行异步操作时需要将它发送到几乎所有其他函数,为什么它不能在第一次“绑定(bind
我正在使用boost::io_service构建并行执行计算作业的线程池。有些作业不允许同时运行,我认为这是boost::io_service::strand的理想应用。作为,执行顺序作业的顺序与无关
我正在编写一个网关服务,它监听网络套接字并将接收到的数据包路由到单独的守护进程。我打算使用 boost asio,但我遇到了几个问题。这是我计划实现的服务器的设计: 网关将使用 boost asio
我正在尝试实现一个包含两个处理循环的 C++ 应用程序。当前,第一个处理循环(boost 的 io_service::run)阻止了第二个处理循环的执行。 利用线程或 std::async 方法的方法
我正在尝试学习 io_service 并使用共享指针。我希望代码无限地工作,直到我像这样调用 stop 方法或某事。不幸的是,在屏幕上看到 workHandler 的输出后,程序关闭了。任何人都可以解
我更喜欢使用 io_service 的方式是在应用程序启动时创建一个线程来执行 io_service的运行方法。问题是,如果 io_service 没有任务,它的 run 方法立即返回并且线程终止。
我是一名优秀的程序员,十分优秀!