什么是使用Boost.Asio的时候确保有效的对象生命周期的最佳方式? [英] What's the best way of ensuring valid object lifespan when using Boost.Asio?

查看:169
本文介绍了什么是使用Boost.Asio的时候确保有效的对象生命周期的最佳方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已经打了很多的后期Boost.Asio的。我喜欢图书馆了很多,因为它提供了一个很好的方式去挤表现出来今天的多核系统。

Been playing a lot with Boost.Asio of late. I like the library a lot since it offers a fantastic way to squeeze performance out of today's multicore systems.

有一个问题,我问自己几次,我以为值得抛出使得与异步调用短耳时,有问候的对象生命周期/所有权。

A question I have asked myself a few times, and I thought worth throwing out there regards object lifespan / ownership when making async calls with Asio.

我已经多次来到翻过的问题是,你往往不得不过期仍然有反对未决的异步回调的对象。如果该对象超出范围的回调调用之前的事情不可避免地一声巨响。

The problem I've come accross repeatedly is that you quite often have to "expire" an object that still has async callbacks pending against it. If that object goes out of scope before the callback is invoked things inevitably go bang.

为了解决这个问题我已经采取了使用的boost :: enable_shared_from_this 模板作为最ASIO基于类的基类。这工作正常,但它是一个有点沉重的负担:平时这也意味着保护构造并添加工厂方法的类,以确保所有实例都一个shared_ptr内部创建

To combat this I've taken to using the boost::enable_shared_from_this template as a base class for most asio based classes. This works OK but it's a little burdensome: usually this also means protecting the constructor and adding a factory method to the class to ensure that all instances are created inside a shared_ptr.

我只是想知道其他人是如何解决这个问题。我要对这个的最好方法?还是我得到了我的Asio.Foo都错了?

I just wanted to know how other people had tackled this problem. Am I going about this the best way? Or have I got my Asio.Foo all wrong?

讨论...:)

推荐答案

使用的boost :: enable_shared_from_this 是pretty大部分做的方式。此外,看看使用的boost :: weak_ptr的如果你需要的对象,不应该preserve的对象,如果它们是仍然是唯一的参考引用。

Using boost::enable_shared_from_this is pretty much the way to do it. Additionally, look at using boost::weak_ptr if you need references to the object that should not preserve the object if they are the only references which remain.

使用的一个很好的例子的weak_ptr :我用 enable_shared_from_this 在我的Socket类,它利​​用的boost :: ASIO 。在的boost :: ASIO 框架是存储对象持久引用,通过读写处理程序的唯一的事情。因此,当插座的析构函数被调用时,我知道,关闭套接字,并在处理程序,我可以做的东西清理为封闭的插座。它使用应用程序的插槽只有一个的weak_ptr 引用,它提升到一个当它要的shared_ptr 与插座的工作(通常是写入)。晋升可以检查故障的情况下,插座走了,虽然插座收盘处理程序通常清理所有的的weak_ptr 引用,即使发生适当了。

A good example of using weak_ptr: I use enable_shared_from_this in my socket class which utilizes boost::asio. The boost::asio framework is the only thing that stores persistent references to the object, via read and write handlers. Thus, when the socket's destructor is called, I know that the socket is closed and I can "do stuff" in a handler to clean up for that closed socket. The application which uses the socket only has a weak_ptr reference to it, which it promotes to a shared_ptr when it wants to work with the socket (usually to write to it). That promotion can be checked for failure in case the socket went away, although the socket's close handler usually cleans up all the weak_ptr references appropriately before that even happens.

这篇关于什么是使用Boost.Asio的时候确保有效的对象生命周期的最佳方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆