软(非:弱)在C ++中的引用 - 这可能吗?有没有执行? [英] Soft (not: weak) references in C++ - Is it possible? Is there an implementation?

查看:105
本文介绍了软(非:弱)在C ++中的引用 - 这可能吗?有没有执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中我使用的boost :: shared_ptr的的boost :: weak_ptr的来自动删除对象不再需要。我知道有引用计数这些工作。

In C++ I'm using boost::shared_ptr and boost::weak_ptr to automatically delete objects that are no longer needed. I know these work with reference counting.

在Java中,内存是由垃圾收集器,它consideres管理的内置对象引用作为的强烈的WeakReference 的和的 SoftReference的 作为介于两者之间(可以由GC收集,但可能还有生存的GC)这是缓存对象有一段时间了,但离尽快把他们作为自由存储空间不足时真的很方便。

In Java, memory is managed by a garbage collector, which consideres the built-in object references as strong, WeakReference as weak and SoftReference as something in between (may be collected by the GC, but may as well survive the GC), which is really handy for caching objects for some time, but throwing them away as soon as free memory is getting low.

所以,现在我回到了C ++和我怀念软引用的舒适度。我不知道如果软引用是切实可行的引用计数的。当最后的强烈的参考对象被清除,仍然存在着的的参考,当它会终究删除?我能想到的一些方案,但他们都不聪明给我。

So now I'm back in C++ and I miss the comfort of having soft references. I wonder if soft referencing is practicable with reference counting at all. When the last strong reference to an object is cleared, and there remains a soft reference, when would it be deleted after all? I could think of some schemes, but none of them seem clever to me.

以防万一没有用于引用计数沿软引用正确的语义,我不知道这是否已经落实,也许在某种程度上与的boost :: shared_ptr的(或C ++ TR1相当于的std :: shared_ptr的为此事)。

Just in case there are proper semantics for soft references along with reference counting, I wonder if this has already been implemented, maybe in a way that's even compatible with boost::shared_ptr (or the C++ TR1 equivalent std::shared_ptr for that matter).

如果回答这两个问题没有,什么都在一个对象缓存方案选择呢?

If the answer to both questions is no, what are the alternatives in an object caching scenario?

编辑:
当然,我说的是,当缓存实际上是有用的情况,因为这些对象的昂贵的构造(想想几个访问数据库和网络查询),竟然有太多让他们永远都

Of course I'm talking of a situation when caching is actually useful, because the objects are costly to construct (think of several access to a database and queries of a network), yet there are too many to keep them all forever.

推荐答案

正如其他人指出的那样,你可以找到的引用计数指针(以及随之而来的 的同行的)的Boost库,但什么是从的软引用的想法是缺少的运行时环境的内存限制,一些意识。在Java中,例如, SoftReference的是没有重大差异在的WeakReference 在其能力;相反,它的合同为运行时会如何 preserve或逐出的两种引用在面对内存pressure的不同的。

As others pointed out, you can find referenced counted pointers (and their attendant weak counterparts) in the Boost library, but what's missing from the soft reference idea is some awareness of the runtime environment's memory constraints. In Java, for example, a SoftReference isn't materially different from a WeakReference in its capabilities; rather, it's the contract for how the runtime will preserve or evict the two kinds of references in the face of memory pressure that differs.

为了模仿C ++这种行为,你必须建立一个在对象上举行的强引用的您的应用程序的其余部分将持有的弱内存感知引用缓存的。当缓存确定应用程序划伤其内存使用量上限 - 或任何其他限制条件 - 这将释放强引用,投降的收藏(到达零引用计数)的对象,并允许使用弱引用后检测无效。

In order to mimic this behavior in C++, you'd have to build a memory-aware reference cache that held strong references on objects that the rest of your application would hold weakly. When the cache determined that the application was scratching its memory usage ceiling — or any other constraining criteria — it would release the strong references, surrendering the objects for "collection" (reaching the zero reference count) and allowing the weak references in use to later detect invalidation.

这篇关于软(非:弱)在C ++中的引用 - 这可能吗?有没有执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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