等待异步操作完成

发布于 2024-10-25 07:54:34 字数 158 浏览 4 评论 0原文

大家好!

有一个 io_service 对象,方法 run() 在线程池中工作。 有一组类,使用 ip::tcp::socket 进行异步写入。 我需要阻止此类的析构函数,直到他的任务存在于 io_service 的队列中。 例如,添加方法 join()。但是阻止析构函数的原因是什么?

HI all!

There is an io_service object, methods run() works in a thread pool.
There are set of classes, which make async write using ip::tcp::socket.
I need to block destructor of this kind of class, until his tasks live in the io_service's queue.
In example, to add method join(). But what the reason to block the destructor?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我不在是我 2024-11-01 07:54:34

我不确定我是否正确理解您的问题,但听起来您想确保等待未完成的异步操作的类在操作完成之前不会被销毁?

enable_shared_from_this 习惯用法可以用于此目的,并且是例如在 http://www. boost.org/doc/libs/1_46_1/doc/html/boost_asio/tutorial/tutdaytime7/src.html。如果您查看 tcp_connection::start 您会看到该类如何将shared_ptr传递给自身以绑定为异步操作的处理程序。

I'm not sure if I understand your question correctly, but it sounds like you want to make sure that classes that are waiting for outstanding async operations are not destroyed before the operation completes?

The enable_shared_from_this idiom can be used for this purpose and is demonstrated e.g. in http://www.boost.org/doc/libs/1_46_1/doc/html/boost_asio/tutorial/tutdaytime7/src.html. If you look at tcp_connection::start you see how the class passes a shared_ptr to itself to bind as the handler for the async operation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文