简体   繁体   English

保持提升 asio io_service

[英]Keep boost asio io_service

I usually keep one io_service object for the whole application and use a number of threads to call run on them.我通常为整个应用程序保留一个 io_service object 并使用多个线程来调用它们的运行。 Creating sockets or timers use the io_service by reference.通过引用创建 sockets 或计时器使用 io_service。 What happens when all threads finish at application exit and there is, lets say a shutdown or cancel operation called on a tcp socket.当所有线程在应用程序退出时完成并且存在,比如说在 tcp 套接字上调用的关闭或取消操作时会发生什么。 The io_service is no longer available. io_service 不再可用。 Is there a copy of the io_service when passing that to any asio related classed or is that undefined behavior?将 io_service 传递给任何与 asio 相关的类时是否存在 io_service 的副本,或者是未定义的行为?

There is never a copy of io_service (or the more recent io_context that replaces the deprecated io_service ).永远不会有io_service的副本(或更新的io_context替换已弃用的io_service )。 That's because it's not copyable.那是因为它不可复制。

If you don't maintain the lifetime while async operations have not completed, the behaviour is indeed undefined.如果您在异步操作尚未完成时不维护生命周期,则行为确实是未定义的。 However, it's not too hard to make sure no async operations/completions happen by stopping the service forcibly gracefully awaiting for it to complete any pending operations.但是,通过强制停止服务以优雅地等待它完成任何挂起的操作来确保不发生异步操作/完成并不难。

I suggest the latter approach for production quality code.我建议后一种方法用于生产质量代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM