等待异步操作完成
大家好!
有一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定我是否正确理解您的问题,但听起来您想确保等待未完成的异步操作的类在操作完成之前不会被销毁?
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.
像这样解决: http://liveworkspace.org/code/7665b7170b3311085fff2e84710d4350
谢谢大家。
Solved like this: http://liveworkspace.org/code/7665b7170b3311085fff2e84710d4350
Thanks for all.