shared_ptr:它是用来做什么的
我在代码中大量使用了 boost::scoped_ptr ,这很棒,但我目前正在使用到处都使用共享指针的软件,我想知道我是否遗漏了一些东西。
据我所知,只有当不同的线程要访问相同的数据并且您不知道线程将按什么顺序完成时,shared_ptr才有用(使用shared_ptr确保对象存在,直到最后一个线程完成它)。
还有其他用例吗?
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.
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).
Are there other use cases?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
线程在这里无关紧要。 相关的是是否容易指定对象不再使用的点。
假设几个不同的对象想要使用同一个对象。 它可能是一组数据,或者用于输入/输出,或者一些几何对象,或者其他什么。 您希望在删除所有正在使用的对象之后删除共享对象,而不是在一个时钟周期之前。 您可以使用shared_ptr 来强制执行此行为,而不是弄清楚哪个所属对象的生命周期最长(如果您更改程序,或者可能通过用户交互,情况可能会发生变化)。
使用对象是在相同线程还是不同线程中并不重要。 即使对象都在同一个线程中,它们也可能具有不可预测的生命周期。
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.
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.
嗯,这是针对多个所有者拥有智能指针指向的同一对象的情况。 它们可以从不同的线程访问智能指针,并且 shared_ptr 也可以在该区域使用,但这不是重点。 如果最后一个所有者失去了对所指向对象的引用,则shared_ptr机制将删除该对象。
如果您想要的只是一个在创建它的作用域离开时被删除的指针(无论是通过异常,还是通过指向某个位置的goto),您可以使用scoped_ptr外部,或通过正常的控制流或其他一些机制)。 如果你这样使用它,就不需要更改为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.
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(和auto_ptr)之间的区别主要在于复制语义。
The difference between scoped_ptr and shared_ptr (and auto_ptr) is mainly copy semantics.
shared_ptr 和scoped_ptr 之间的另一个重要区别是只有shared_ptr 可以与weak_ptr 一起使用。 弱指针用于打破共享指针的循环,从而避免内存泄漏,但weak_ptr 的用途还不止于此。
共享指针和弱指针可用于表达拥有引用和非拥有引用之间的差异。 明确的数据所有权会带来更简洁的设计,因此,当可能的情况下,数据对象应通过shared_ptr 由一个其他对象拥有。 对数据对象的所有其他长期引用都应该是弱指针,表达它们对数据的非所有权。 每次任何非拥有模块访问数据时,它们都需要将weak_ptr转换为shared_ptr,此时它们可能会发现该数据对象不再存在。 然而,当非拥有模块访问数据对象时,它们通过瞬态shared_ptr保存它,即使拥有对象要释放数据也确保安全操作。
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 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 是关于共享所有权的。 然而,我认为共享所有权通常是一件坏事(存在例外,例如享元模式),最好识别所有者并在那里放置一个scoped_ptr。
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 是一种执行引用计数的智能指针类型。 如果该对象只有一个所有者(常见情况),那么scoped_ptr 是正确的解决方案。 如果该对象可以在代码的多个部分之间共享,则在释放对该对象的所有引用之前,shared_ptr 不会让该对象被破坏。
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.