是否有诸如weak_ptr之类的东西无法锁定(提升为shared_ptr)?如果没有,为什么? [英] Is there such thing as a weak_ptr that can't be locked (promoted to shared_ptr)? If not, why?

查看:193
本文介绍了是否有诸如weak_ptr之类的东西无法锁定(提升为shared_ptr)?如果没有,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许以前有人问过这个问题,但我从未找到满意的答案.另外,为简单起见,假设我在谈论单线程应用程序.

Maybe this question has been asked before, but I've never found a satisfactory answer. Also, for the purposes of simplicity assume I'm talking about a single-threaded application.

所以,我听到过很多次,如果您有一个不拥有并且其使用寿命可以保证的对象,则应该引用它带有原始指针.对象的所有者将使用unique_ptr,并根据需要分发原始指针.

So, what I've heard a number of times is that if you have an object that is non-owned and whose lifetime is guaranteed, you should reference it with a raw pointer. The object's owner would use a unique_ptr, and hand out raw pointers as necessary.

但是,如果对象是非所有者,并且生存期不能保证 ,该怎么办?然后可以使用weak_ptr,是的.但是,随后任何人都可能会收到一个weak_ptr顽皮并将其锁定,从而使对象的所有者无法导致该对象被破坏.有时这可能不是问题,但有时是问题.例如,当拥有的对象表示必须在特定时间放弃的某些系统资源时.

But what if the object is non-owned, and the lifetime is not guaranteed? Then you can use a weak_ptr, yes. But then anyone who is handed a weak_ptr could be naughty and keep it locked, such that the object's owner can't cause the object to be destroyed. Sometimes this may not be a problem, but sometimes it is. For example, when the owned object represents some system resource which must be relinquished at a certain time.

您可能会说:好吧,那么您应该确保没有人将weak_ptr保持锁定状态!"但这从OO设计的角度来看并不理想(因为我认为),因为这会在所有者"对象和从中获取"weak_ptr"的任何对象之间建立依赖关系.您也可以将参数设为您不需要返回const引用;您应该确保没有人修改该引用."

You may say "well, then you should just make sure no one keeps the weak_ptr locked!" But that is just not ideal (in my opinion) from an OO design standpoint, as it creates a dependency between the "owner" object and any object that gets a weak_ptr from it. You might as well make the argument "you don't need to return const references; you should just make sure no one modifies the reference."

有了Qt,您就有了QPointer,这基本上就是我想要的.它检查对象是否尚未销毁,但不能防止销毁该对象.我意识到这不是线程安全的,但是我还是在谈论单个线程的上下文.

With Qt, you have the QPointer, which is basically what I'm looking for. It checks that the object hasn't been destroyed, but it can't prevent the object from being destroyed. I realize this isn't thread-safe, but again, I'm talking about the context of a single thread.

那么为什么C ++ 11没有类似的东西?我敢肯定,我可以为weak_ptr做一个包装,完成我的追求.但是我想知道我是否要解决所有这些错误.

So why isn't there something similar for C++11? I'm sure I could make a wrapper around weak_ptr that accomplishes what I'm after. But I wonder if I'm going about this all wrong.

推荐答案

您可以使用shared_ptr<unique_ptr<T>>使用纯标准C ++进行此操作.

You can do this with pure Standard C++ using shared_ptr<unique_ptr<T>>.

观察者仅收到一个shared_ptr<const unique_ptr<T>>,使他们可以看但不能触摸.具有非const智能指针的所有者可以随时调用unique_ptr上的reset()销毁实例.那时,所有观察者还可以看到unique_ptr已为空.

Observers received only a shared_ptr<const unique_ptr<T>>, allowing them to look but not touch. The owner, having a non-const smart pointer, can at any time call reset() on the unique_ptr to destroy the instance. At that time all the observers can also see that the unique_ptr has become empty.

适用于明显的线程和重入警告(您需要在每次调用回调后检查unique_ptr再次具有有效的指针,等等).

Obvious threading and re-entrance caveats apply (you need to check the unique_ptr for having a valid pointer again after each callback you invoke, etc).

如果应该有多个所有者,则需要做更多的工作.您将需要一个shared_ptr<T*>,给观察者一个shared_ptr<T* const>.还有一个单独的shared_ptr<T>来管理对象的生存期. shared_ptr<T*>将需要在对象的析构函数中手动填充nullptr(T*,而不是shared_ptr).

And if there should be multiple owners, it's a bit more work. You will need one shared_ptr<T*>, giving observers a shared_ptr<T* const>. And a separate shared_ptr<T> to manage the object lifetime. The shared_ptr<T*> will need to be manually filled with nullptr (The T*, not the shared_ptr) in the object's destructor.

这篇关于是否有诸如weak_ptr之类的东西无法锁定(提升为shared_ptr)?如果没有,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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