等待完成异步操作

[英]Waiting for completing of asynchronous operations


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?

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

2 个解决方案

#1


0  

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.

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传递给它自己以作为异步操作的处理程序进行绑定。

#2


0  

Solved like this: http://liveworkspace.org/code/7665b7170b3311085fff2e84710d4350

像这样解决:http://liveworkspace.org/code/7665b7170b3311085fff2e84710d4350

Thanks for all.

谢谢大家。

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2011/03/22/2690ad87f8fab4b404806733125161fb.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告