如何weak_ptr的工作? [英] How does weak_ptr work?

查看:140
本文介绍了如何weak_ptr的工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何使用的weak_ptr 的shared_ptr 。我了解的shared_ptr 的作品,在其统计对象的引用数。如何的weak_ptr 工作?我试图通过升压源$ C ​​$ C读书,我不熟悉的刺激不够了解它使用的所有的东西。

I understand how to use weak_ptr and shared_ptr. I understand how shared_ptr works, by counting the number of references in its object. How does weak_ptr work? I tried reading through the boost source code, and I'm not familiar with boost enough to understand all the things it uses.

感谢。

推荐答案

的shared_ptr 使用一个额外的计数器对象(又名共享计数或控制块 )来存储的引用计数。
(顺便说一句:那反的对象也存储的删除)

shared_ptr uses an extra "counter" object (aka. "shared count" or "control block") to store the reference count. (BTW: that "counter" object also stores the deleter.)

每个的shared_ptr 的weak_ptr 包含一个指向实际的指针对象,第二个指向柜台对象。

Every shared_ptr and weak_ptr contains a pointer to the actual pointee, and a second pointer to the "counter" object.

要落实的weak_ptr 中,反对象存储两个不同的计数器:

To implement weak_ptr, the "counter" object stores two different counters:


  • 将使用计数,是的shared_ptr 指向的对象实例的数量。

  • 的弱计数是的weak_ptr 指向的对象的实例的数量,加一,如果使用计数仍然是> 0

  • The "use count" is the number of shared_ptr instances pointing to the object.
  • The "weak count" is the number of weak_ptr instances pointing to the object, plus one if the "use count" is still > 0.

指针对象获取的时候,使用计数达到零删除。

The pointee get's deleted when the "use count" reaches zero.

在柜台帮助对象获取的时候,弱计数达到零删除(这意味着使用计数也必须是零,见上文)。

The "counter" helper object get's deleted when the "weak count" reach zero (which means the "use count" must also be zero, see above).

当您试图获取一个的shared_ptr 的weak_ptr ,图书馆原子检查使用计数如果它> 0递增它。如果成功你得到你的的shared_ptr 。如果使用计数已经为0,你会得到一个空的的shared_ptr 实例,而不是

When you try to obtain a shared_ptr from a weak_ptr, the library atomically checks the "use count", and if it's > 0 increments it. If that succeeds you get your shared_ptr. If the "use count" was already zero you get an empty shared_ptr instance instead.

修改:现在,为什么他们增加一个弱计数,而不仅仅是释放反的对象时,这两个方面降为零?好问题。

EDIT: Now, why do they add one to the weak count instead of just releasing the "counter" object when both counts drop to zero? Good question.

另一种方法是删除计数器对象时,无论是使用计数和弱伯爵降为零。这是第一个原因:检查两(指针大小)计数器原子不可能每个平台上,甚至连它在哪里,它比检查只是一个柜台更复杂

The alternative would be to delete the "counter" object when both the "use count" and the "weak count" drop to zero. Here's the first reason: Checking two (pointer sized) counters atomically is not possible on every platform, and even where it is, it's more complicated than checking just one counter.

另外一个原因是,直到它已完成执行的删除必须保持有效。由于删除器被存储在计数对象,这意味着计数对象必须保持有效。考虑会发生什么,如果有一个的shared_ptr 和一个的weak_ptr 一些对象,他们在同一时间重置在并发线程。比方说,的shared_ptr 第一。它减少了使用计数到零,并开始执行该删除器。现在的weak_ptr 降低弱计数为零,并找到使用计数是零。因此,删除了反对象,并使用它的删除。虽然删除器仍在运行。

Another reason is, that the deleter must stay valid until it has finished executing. Since the deleter is stored in the "counter" object, that means the "counter" object must stay valid. Consider what could happen if there is one shared_ptr and one weak_ptr to some object, and they are reset at the same time in concurrent threads. Let's say the shared_ptr comes first. It decreases the "use count" to zero, and begins executing the deleter. Now the weak_ptr decreases the "weak count" to zero, and finds the "use count" is zero as well. So it deletes the "counter" object, and with it the deleter. While the deleter is still running.

当然会有不同的方法以保证反的对象保持活着,但我认为一个增加弱伯爵是一个非常优雅和直观的解决方案。 弱计数变成为计数的对象的引用计数。而且,由于的shared_ptr 参考中的计数器对象也一样,他们也有递增的弱计数。

Of course there would be different ways to assure that the "counter" object stays alive, but I think increasing the "weak count" by one is a very elegant and intuitive solution. The "weak count" becomes the reference count for the "counter" object. And since shared_ptrs reference the counter object too, they too have to increment the "weak count".

一个甚至可能是更直观的解决方案将是增加弱计数为每一个的shared_ptr ,因为每一个的shared_ptr 举办的以反对象的引用。

A probably even more intuitive solution would be to increment the "weak count" for every single shared_ptr, since every single shared_ptr hold's a reference to the "counter" object.

添加一个用于所有的shared_ptr 情况下仅仅是一个优化(节省一个原子递增/递减当复制/分配的shared_ptr 实例)。

Adding one for all shared_ptr instances is just an optimization (saves one atomic increment/decrement when copying/assigning shared_ptr instances).

这篇关于如何weak_ptr的工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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