Weak resources and factory design pattern

ⅰ亾dé卋堺 提交于 2019-12-06 09:56:07

I think you may want to consider turning it around : give away the ownership of objects you have created (e.g. returning boost::shared_ptr<Bitmap>) and only retain weak pointers to objects in your internal collection (e.g. std::list<boost::weak_ptr<Bitmap>>). Whenever user asks for a Bitmap, do a lock on your collection and add a new Bitmap there. If your collection is a list you can safely store iterator to just added element and remove it from the collection later, thus handling destruction of a Bitmap.

Since boost::shared_ptr::lock is atomic, it will also work when multithreading. But you still need to guard access to collection with a lock.

Here is sample code:

#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>
#include <boost/noncopyable.hpp>
#include <boost/thread.hpp>

class Display;

class Bitmap : boost::noncopyable
{
  friend class Display;
  Bitmap(int, Display* d) : display(d)
  {}

  Display* display;
public:

  ~Bitmap(); // see below for definition
};

typedef boost::shared_ptr<Bitmap> BitmapPtr;

class Display
{
  typedef std::list<boost::weak_ptr<Bitmap>> bitmaps_t;
  typedef std::map<Bitmap*, bitmaps_t::iterator> bitmap_map_t;
  bitmaps_t     bitmaps_;
  bitmap_map_t  bitmap_map_;
  boost::mutex  mutex_;

  friend class Bitmap;
  void Remove(Bitmap* p)
  {
    boost::lock_guard<boost::mutex> g(mutex_);
    bitmap_map_t::iterator i = bitmap_map_.find(p);
    if (i != bitmap_map_.end())
    {
      bitmaps_.erase(i->second);
      bitmap_map_.erase(i);
    }
  }

public:
  ~Display()
  {
    boost::lock_guard<boost::mutex> g(mutex_);
    for (bitmaps_t::iterator i = bitmaps_.begin(); i != bitmaps_.end(); ++i)
    {
      BitmapPtr ptr = i->lock();
      if (ptr)
        ptr->display = NULL;
    }
  }

  BitmapPtr GetBitmap(int i)
  {
    BitmapPtr r(new Bitmap(i, this));
    boost::lock_guard<boost::mutex> g(mutex_);
    bitmaps_.push_back(boost::weak_ptr<Bitmap>(r));
    bitmap_map_[r.get()] = --bitmaps_.end();
    return r;
  }
};

Bitmap::~Bitmap()
{
  if (display)
    display->Remove(this);
}

Please note there is two-way link between Bitmap and Display - they both keep a weak pointer to each other, meaning they can freely communicate. For example, if you want Display to be able to "destroy" Bitmaps, it can simply disable them by updating "display" to NULL, as demonstrated in Display destructor. Display has to lock a weak_ptr<Bitmap> every time it wants to access the Bitmap (example in destructor) but this shouldn't be much of a problem if this communication does not happen often.

For thread safety you may need to lock Bitmap everytime you want to "disable" it i.e. reset display member to NULL, and lock it everytime you want to access "display" from Bitmap. Since this has performance implications, you might want to replace Display* with weak_ptr<Display> in Bitmap. This also removes need for destructor in Display. Here is updated code:

class Bitmap : boost::noncopyable
{
  friend class Display;
  Bitmap(int, const boost::shared_ptr<Display>& d) : display(d)
  {}

  boost::weak_ptr<Display> display;

public:
  ~Bitmap(); // see below for definition
};

typedef boost::shared_ptr<Bitmap> BitmapPtr;

class Display : public boost::enable_shared_from_this<Display>
              , boost::noncopyable
{
  //... no change here

public:
  BitmapPtr GetBitmap(int i)
  {
    BitmapPtr r(new Bitmap(i, shared_from_this()));
    boost::lock_guard<boost::mutex> g(mutex_);
    bitmaps_.push_back(boost::weak_ptr<Bitmap>(r));
    bitmap_map_[r.get()] = --bitmaps_.end();
    return r;
  }
};

Bitmap::~Bitmap()
{
  boost::shared_ptr<Display> d(display);
  if (d)
    d->Remove(this);
}

In this case "disabling" display pointer in Bitmap is thread safe without explicit synchronisation, and looks like this:

Display::DisableBitmap(bitmaps_t::iterator i)
{
  BitmapPtr ptr = i->lock();
  if (ptr)
    ptr->display.reset();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!