shared_ptr:它用于什么

[英]shared_ptr: what's it used for


I make a lot of use of boost::scoped_ptr in my code and it is great but I'm currently working with software that uses shared_ptr all over the place and I'm wondering if I'm missing something.

我在我的代码中大量使用了boost :: scoped_ptr,它很棒,但我目前正在使用整个地方使用shared_ptr的软件,我想知道我是否遗漏了一些东西。

AFAIK a shared_ptr is only useful if different threads are going to be accessing the same data and you don't know what order the threads are going to finish (with the shared_ptr ensuring that the object exists until the last thread has finished with it).

AFAIK a shared_ptr仅在不同的线程要访问相同的数据时才有用,并且您不知道线程将完成的顺序(使用shared_ptr确保对象存在,直到最后一个线程完成它为止)。

Are there other use cases?

还有其他用例吗?

6 个解决方案

#1


7  

Threads are irrelevant here. What's relevant is whether it's easy to specify a point at which the object is no longer of use.

线程在这里无关紧要。有意义的是,是否容易指定对象不再使用的点。

Suppose several different objects want to use the same object. It might be a pack of data, or for input/output, or some geometric object, or whatever. You want the shared object to be deleted after all of the using objects are deleted, and not a clock cycle before. Rather than figure out which owning object is going to have the longest lifespan (and that can change if you change the program, or perhaps through user interaction), you can use a shared_ptr to force this behavior.

假设有几个不同的对象想要使用同一个对象。它可能是一组数据,或输入/输出,或某些几何对象,或其他任何东西。您希望在删除所有使用对象后删除共享对象,而不是之前的时钟周期。您可以使用shared_ptr来强制执行此行为,而不是确定哪个拥有对象的生命周期最长(如果您更改程序,或者可能通过用户交互,则可以更改)。

It doesn't matter whether the using objects are in the same or different threads. Objects can have unpredictable lifetimes even if they're all in the same thread.

使用对象是在相同还是不同的线程中无关紧要。对象可能具有不可预测的生命周期,即使它们都在同一个线程中。

#2


7  

AFAIK a shared_ptr is only useful if different threads are going to be accessing the same data

AFAIK a shared_ptr仅在不同的线程要访问相同数据时才有用

Well, it's for situations where multiple owners own the same object pointed to by the smart pointer. They may access the smart pointers from different threads, and shared_ptr is usable in that area too, but that's not the main point. If the last owner loses its reference to the object pointed to, the shared_ptr mechanism deletes the object.

嗯,这适用于多个所有者拥有智能指针指向的相同对象的情况。他们可以从不同的线程访问智能指针,而shared_ptr也可以在该区域中使用,但这不是主要观点。如果最后一个所有者丢失了对指向的对象的引用,则shared_ptr机制将删除该对象。

You can use a scoped_ptr if all you want to have is a pointer that is deleted when the scope it's created in is left (either by exceptions, by a goto to a place outside, or by normal control flow or some other mechanism). If you use it like that, there is no need to change to shared_ptr.

你可以使用scoped_ptr,如果你想拥有的是一个指针,当它被创建的范围被删除时(通过异常,转到外部的地方,或通过正常的控制流或其他机制)。如果您这样使用它,则无需更改为shared_ptr。

#3


2  

The difference between scoped_ptr and shared_ptr (and auto_ptr) is mainly copy semantics.

scoped_ptr和shared_ptr(和auto_ptr)之间的区别主要是复制语义。

  • scoped_ptr is for "Resource Allocation Is Initialization" and is not copyable (it cannot be shared with other instances and ownership cannot be transferred)
  • scoped_ptr用于“资源分配是初始化”并且不可复制(不能与其他实例共享,并且无法传输所有权)

  • shared_ptr is for automatic reclamation of memory when shared between multiple parties
  • shared_ptr用于在多方之间共享时自动回收内存

  • auto_ptr is copyable (and transfers ownership when assigned)
  • auto_ptr是可复制的(并在分配时转移所有权)

#4


2  

Another important difference between shared_ptr and scoped_ptr is that only shared_ptr work with weak_ptr. Weak pointers are used to break cycles of shared pointers, thereby avoiding memory leaks, but weak_ptr can be used for more than that.

shared_ptr和scoped_ptr之间的另一个重要区别是,只有shared_ptr可以与weak_ptr一起使用。弱指针用于打破共享指针的循环,从而避免内存泄漏,但weak_ptr可用于更多。

Shared and weak pointers may be used to express the difference between owning and non-owning references. Unambiguous ownership of data leads to a cleaner design, so when possible data objects should be owned by one other object through a shared_ptr. All other long-lived references to data objects should be weak pointers, expressing their non-ownership of the data. Each time any non-owning modules access the data, they need to convert the weak_ptr into a shared_ptr, at which point they may find that the data object no longer exists. However, while the non-owning modules access the data object, they hold it through transient shared_ptr, ensuring safe operation even if the owning object were to release the data.

共享和弱指针可用于表示拥有和非拥有引用之间的差异。明确的数据所有权导致更清晰的设计,因此当可能的数据对象应该由另一个对象通过shared_ptr拥有。所有其他对数据对象的长期引用都应该是弱指针,表示它们对数据的非所有权。每次任何非拥有模块访问数据时,他们都需要将weak_ptr转换为shared_ptr,此时他们可能会发现数据对象不再存在。但是,当非拥有模块访问数据对象时,它们通过瞬态shared_ptr保持它,即使拥有对象要释放数据,也确保安全操作。

#5


1  

As answered already, shared_ptr is about shared ownership. However, I would argue that shared ownership is generally a bad thing (exceptions exists, such as flyweight pattern) and it is better to identify an owner and put a scoped_ptr there.

正如已经回答的那样,shared_ptr是关于共享所有权的。但是,我认为共享所有权通常是一件坏事(存在例外情况,例如flyweight模式),最好识别一个所有者并在其中放置一个scoped_ptr。

#6


1  

A shared_ptr is a smart pointer type that does reference counting. If there's only one owner for the object (frequent case), then scoped_ptr is the right solution. If the object can be shared among multiple parts of the code, then shared_ptr won't let the object be destructed until all references to it have been released.

shared_ptr是一个引用计数的智能指针类型。如果对象只有一个所有者(常见情况),则scoped_ptr是正确的解决方案。如果对象可以在代码的多个部分之间共享,那么shared_ptr将不会破坏对象,直到释放了对它的所有引用。

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2008/12/05/40d0ee437a240ca2548fd647f581306.html



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

赞助商广告