如何管理对象的生命时间使用Boost库智能指针? [英] How to manage object life time using Boost library smart pointers?

查看:122
本文介绍了如何管理对象的生命时间使用Boost库智能指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个场景,我需要解决与shared_ptr和weak_ptr智能指针。

There is a scenario that i need to solve with shared_ptr and weak_ptr smart pointers.

两个线程,线程1& 2,正在使用一个称为A的共享对象。每个线程都有对该对象的引用。线程1决定删除对象A,但是同时线程2可能正在使用它。如果我使用shared_ptr来保存对象A在每个线程中的引用,对象不会在正确的时间被删除。

Two threads, thread 1 & 2, are using a shared object called A. Each of the threads have a reference to that object. thread 1 decides to delete object A but at the same time thread 2 might be using it. If i used shared_ptr to hold object A's references in each thread, the object wont get deleted at the right time.

我应该怎么做才能删除对象

What should i do to be able to delete the object when its supposed to and prevent an error in other threads that using that object at the same time?

推荐答案

有两种情况:

如果thread1是对象的所有者,thread2需要使用它,在thread2中存储一个weak_ptr。弱指针不参与引用计数,而是如果对象仍然存在,它们提供一种访问对象的shared_ptr的方法。如果对象不存在,weak_ptr将返回一个空/ null shared_ptr。

If thread1 is the "owner" of the object and thread2 needs to just use it, store a weak_ptr in thread2. Weak pointers do not participate in reference counting, instead they provide a way to access a shared_ptr to the object if the object still exists. If the object doesn't exist, weak_ptr will return an empty/null shared_ptr.

下面是一个例子:

  class CThread2
  {
  private:
      boost::weak_ptr<T> weakPtr
  public:
   void SetPointer(boost::shared_ptr<T> ptrToAssign)
   {
        weakPtr = ptrToAssign;
   }

   void UsePointer()
   {
        boost::shared_ptr<T> basePtr;
        basePtr = weakPtr.lock()
        if (basePtr)
        {
             // pointer was not deleted by thread a and still exists,
             // so it can be used.
        }
        else
        {
             // thread1 must have deleted the pointer
        }
   }
  };

我对此问题的回答(链接)也可能是有用的。

My answer to this question (link) might also be useful.

如果你的任何一个线程可以执行删除,比你不能有我上面描述的。由于除了底层对象之外,两个线程都需要知道指针的状态,这可能是一个指向指针的指针有用的情况。

If either of your threads can perform deletion, than you can not have what I describe above. Since both threads need to know the state of the pointer in addition to the underlying object, this may be a case where a "pointer to a pointer" is useful.

 boost::shared_ptr< boost::shared_ptr<T> >

或(通过原始ptr)

shared_ptr<T>* sharedObject;

或只是

T** sharedObject;

为什么有用?


  • 你只有一个引用到T(事实上shared_ptr是很冗余的)

  • 两个线程都可以检查单个共享指针的状态被其他线程删除?)

陷阱:
- 想想当双方都试图删除时间,您可能需要锁定此指针

Pitfalls: - Think about what happens when both sides try to delete at the same time, you may need to lock this pointer

修改示例:

 class CAThread
  {
  private:
      boost::shared_ptr<T>* sharedMemory;
  public:
   void SetPointer(boost::shared_ptr<T>* ptrToAssign)
   {
        assert(sharedMemory != NULL);
        sharedMemory = ptrToAssign;
   }

   void UsePointer()
   {
        // lock as needed
        if (sharedMemory->get() != NULL)
        {
             // pointer was not deleted by thread a and still exists,
             // so it can be used.
        }
        else
        {
             // other thread must have deleted the pointer
        }
   }

   void AssignToPointer()
   {
        // lock as needed
        sharedMemory->reset(new T);
   }

   void DeletePointer()
   {
        // lock as needed
        sharedMemory->reset();
   }
  };

我忽略了底层数据的所有并发问题,但这不是真正的询问。

I'm ignoring all the concurrency issues with the underlying data, but that's not really what you're asking about.

这篇关于如何管理对象的生命时间使用Boost库智能指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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