21

I'm having fun with c++-ideas, and got a little stuck with this problem.

I would like a LIFO class that manages a pool of resources. When a resource is requested (through acquire()), it returns the object as a unique_ptr that, upon deletion, causes the resource to be returned to the pool.

The unit tests would be:

// Create the pool, that holds (for simplicity, int objects)
SharedPool<int> pool;
TS_ASSERT(pool.empty());

// Add an object to the pool, which is now, no longer empty
pool.add(std::unique_ptr<int>(new int(42)));
TS_ASSERT(!pool.empty());

// Pop this object within its own scope, causing the pool to be empty
{
  auto v = pool.acquire();
  TS_ASSERT_EQUALS(*v, 42);
  TS_ASSERT(pool.empty());
}

// Object should now have returned to the pool
TS_ASSERT(!pool.empty())

Basic implementation, which would pass the tests, except for the important final test:

template <class T>
class SharedPool
{
 public:
  SharedPool(){}
  virtual ~SharedPool(){}

  void add(std::unique_ptr<T> t) {
    pool_.push(std::move(t));
  }

  std::unique_ptr<T> acquire() {
    assert(!pool_.empty());
    std::unique_ptr<T> tmp(std::move(pool_.top()));
    pool_.pop();
    return std::move(tmp);
  }

  bool empty() const {
    return pool_.empty();
  }

 private:
  std::stack<std::unique_ptr<T> > pool_;
};

The question: How to go about so that acquire() returns a unique_ptr of a type such that the deleter has knowledge of this, and calls this->add(...), returning the resource back to the pool.

2
  • 2
    If you are using a custom deleter, you are no longer returning a std::unique_ptr<T>. Either fix the signature or use something with a type-erased deleter (such as shared_ptr).
    – T.C.
    Jan 7, 2015 at 20:22
  • I know :), it might be of the type std::unique_ptr<T, std::function<void(T*)> >, but I didn't want to add half-answers. My confusion is more how this is properly combined with std::bind. I'll rely on more experienced C++-devs to fill in the blanks. An alternative, which I wanted to tackle afterwards, was returning a std::shared_ptr, but, if it's properly solved for std::unique_ptr, it's automatically solved for the shared_ptr case.
    – swalog
    Jan 7, 2015 at 20:24

4 Answers 4

23

Naive implementation

The implementation uses unique_ptr with a custom deleter that returns objects to the pool. Both acquire and release are O(1). Additionally, unique_ptr with custom deleters can be implicitly converted to shared_ptr.

template <class T>
class SharedPool
{
 public:
  using ptr_type = std::unique_ptr<T, std::function<void(T*)> >;

  SharedPool() {}
  virtual ~SharedPool(){}

  void add(std::unique_ptr<T> t) {
    pool_.push(std::move(t));
  }

  ptr_type acquire() {
    assert(!pool_.empty());
    ptr_type tmp(pool_.top().release(),
                 [this](T* ptr) {
                   this->add(std::unique_ptr<T>(ptr));
                 });
    pool_.pop();
    return std::move(tmp);
  }

  bool empty() const {
    return pool_.empty();
  }

  size_t size() const {
    return pool_.size();
  }

 private:
  std::stack<std::unique_ptr<T> > pool_;
};

Example usage:

SharedPool<int> pool;
pool.add(std::unique_ptr<int>(new int(42)));
pool.add(std::unique_ptr<int>(new int(84)));
pool.add(std::unique_ptr<int>(new int(1024)));
pool.add(std::unique_ptr<int>(new int(1337)));

// Three ways to express the unique_ptr object
auto v1 = pool.acquire();
SharedPool<int>::ptr_type v2 = pool.acquire();    
std::unique_ptr<int, std::function<void(int*)> > v3 = pool.acquire();

// Implicitly converted shared_ptr with correct deleter
std::shared_ptr<int> v4 = pool.acquire();

// Note that adding an acquired object is (correctly) disallowed:
// pool.add(v1);  // compiler error

You might have caught a severe problem with this implementation. The following usage isn't unthinkable:

  std::unique_ptr< SharedPool<Widget> > pool( new SharedPool<Widget> );
  pool->add(std::unique_ptr<Widget>(new Widget(42)));
  pool->add(std::unique_ptr<Widget>(new Widget(84)));

  // [Widget,42] acquired(), and released from pool
  auto v1 = pool->acquire();

  // [Widget,84] is destroyed properly, together with pool
  pool.reset(nullptr);

  // [Widget,42] is not destroyed, pool no longer exists.
  v1.reset(nullptr);
  // Memory leak

We need a way to keep alive information necessary for the deleter to make the distinction

  1. Should I return object to pool?
  2. Should I delete the actual object?

One way of doing this (suggested by T.C.), is having each deleter keep a weak_ptr to shared_ptr member in SharedPool. This lets the deleter know if the pool has been destroyed.

Correct implementation:

template <class T>
class SharedPool
{
 private:
  struct External_Deleter {
    explicit External_Deleter(std::weak_ptr<SharedPool<T>* > pool)
        : pool_(pool) {}

    void operator()(T* ptr) {
      if (auto pool_ptr = pool_.lock()) {
        try {
          (*pool_ptr.get())->add(std::unique_ptr<T>{ptr});
          return;
        } catch(...) {}
      }
      std::default_delete<T>{}(ptr);
    }
   private:
    std::weak_ptr<SharedPool<T>* > pool_;
  };

 public:
  using ptr_type = std::unique_ptr<T, External_Deleter >;

  SharedPool() : this_ptr_(new SharedPool<T>*(this)) {}
  virtual ~SharedPool(){}

  void add(std::unique_ptr<T> t) {
    pool_.push(std::move(t));
  }

  ptr_type acquire() {
    assert(!pool_.empty());
    ptr_type tmp(pool_.top().release(),
                 External_Deleter{std::weak_ptr<SharedPool<T>*>{this_ptr_}});
    pool_.pop();
    return std::move(tmp);
  }

  bool empty() const {
    return pool_.empty();
  }

  size_t size() const {
    return pool_.size();
  }

 private:
  std::shared_ptr<SharedPool<T>* > this_ptr_;
  std::stack<std::unique_ptr<T> > pool_;
};
16
  • 3
    I'd store the objects in the pool as plain unique_ptr<T>s (no custom deleter), and attach the custom deleter only in the acquire() (which can return either a unique_ptr with a custom deleter or a shared_ptr with a type-erased deleter). This also obviates the need to write a custom destructor.
    – T.C.
    Jan 9, 2015 at 2:46
  • 1
    The constructor of std::function may throw, but unique_ptr requires that its deleter's constructors must not throw, so you might want to use a custom deleter type. Also, your "more robust" version essentially uses a reference-counted smart pointer. There is already such a pointer in the standard library...
    – T.C.
    Jan 9, 2015 at 18:52
  • 2
    The custom deleter still needs to call std::stack<unique_ptr<T>>::push(unique_ptr<T>&&) which can throw, and if that happens inside a unique_ptr destructor it will call std::terminate(), so you need to handle bad_alloc Jan 13, 2015 at 15:21
  • 1
    I've learnt a lot from this answer. One thing that I still dont understand, what is meant by the deference operator (*) in std::weak_ptr<SharedPool<T>* > pool_
    – vibz
    Apr 4, 2018 at 10:02
  • 1
    I ended up combining several of the good ideas in this solution with parts of the PoolableObjectFactory from the Poco library (and using a thread-safe queue instead of a stack). It works for my needs really well. I also want to extend it to custom pointer types and deleters for the pooled objects, which is useful if you want a pool of something like Windows HANDLEs . For now I have to wrap such things in a real class. unique_ptr/shared_ptr allow for such things, but there is a bit of STL magic involved in how the pointer type is inferred. Jul 2, 2020 at 21:01
11

Here's a custom deleter that checks if the pool is still alive.

template<typename T>
class return_to_pool
{
  std::weak_ptr<SharedPool<T>> pool

public:
  return_to_pool(const shared_ptr<SharedPool<T>>& sp) : pool(sp) { }

  void operator()(T* p) const
  {
    if (auto sp = pool.lock())
    {
      try {
        sp->add(std::unique_ptr<T>(p));
        return;
      } catch (const std::bad_alloc&) {
      }
    }
    std::default_delete<T>{}(p);
  }
};

template <class T>
class SharedPool : std::enable_shared_from_this<SharedPool<T>>
{
public:
  using ptr_type = std::unique_ptr<T, return_to_pool<T>>;
  ...
  ptr_type acquire()
  {
    if (pool_.empty())
      throw std::logic_error("pool closed");
    ptr_type tmp{pool_.top().release(), this->shared_from_this()};
    pool_.pop();
    return tmp;
  }
  ...
};

// SharedPool must be owned by a shared_ptr for enable_shared_from_this to work
auto pool = std::make_shared<SharedPool<int>>();
7
  • Would it be possible to explain the std::shared_ptr<SharedPool>{shared, this}. It would be equivalent to the more explicit return_to_pool{std::shared_ptr<SharedPool>{shared, this}}. But, why does this compile? Why does the return_to_pool constructor parameter accept a shared_ptr with SharedPool<T>::dummy, and SharedPool<T>* arguments. What is going on here?
    – swalog
    Jan 14, 2015 at 10:53
  • 1
    @swalog stackoverflow.com/questions/25920681/…
    – T.C.
    Jan 14, 2015 at 22:33
  • I don't understand your use of shared. What prevents SharedPool from going out of life (before dummy goes out of life) before the last ptr_type pointer was destroyed? It seems to me that the weak_ptr could tell you "it's still alife", while it may be not. Usually the aliasing constructor is passed a pointer to a subobject, and a shared_ptr to the surrounding object. But in your case it's kindof the other way around, which confuses me. Oct 3, 2016 at 15:58
  • @litb, you're right it doesn't help with object lifetimes. IIRC I was thinking of cases where your pool is already managed elsewhere (e.g. as a global) and is guaranteed to outlive the deleters referring to it ... but in that case there's no advantage to using weak_ptr to refer to it. That's fragile code though, so it's not a good example. I'll remove it. Oct 4, 2016 at 9:47
  • I want acquire() to return nullptr if pool is empty, but it seems not possible here, since custom deleter is used. I could use a try { } catch {} block, but I think there must be some performance decrease to some extent. Is there a better way to do this?
    – zhy
    Jul 21, 2022 at 7:01
5

Although the question is old and has already been answered, I have one minor comment on the solution proposed by @swalog.

Deleter functor can result in memory corruption due to double deletion:

void operator()(T* ptr) {
  if (auto pool_ptr = pool_.lock()) {
    try {
      (*pool_ptr.get())->add(std::unique_ptr<T>{ptr});
      return;
    } catch(...) {}
  }
  std::default_delete<T>{}(ptr);
}

unique_ptr created here will be destroyed when exception is caught. Hence,

std::default_delete<T>{}(ptr);

will result in double deletion.

It can be fixed by changing a place of creating unique_ptr from T*:

void operator()(T* ptr) {
  std::unique_ptr<T> uptr(ptr);
  if (auto pool_ptr = pool_.lock()) {
    try {
      (*pool_ptr.get())->add(std::move(uptr));
      return;
    } catch(...) {}
  }
}
0
2

Consider using a shared_ptr instead. The only change you'd have to make is to not count auto pointers with more than one owner. Objects that had would aquired from the SharedPool could delete the auto pointer as normal, but the SharedPool would still hold the actual auto pointer.

template <class T>
class SharedPool
{
 public:
  SharedPool(){}
  virtual ~SharedPool(){}

  void add(std::unique_ptr<T> t) {
    pool_.push_back(std::move(t));
  }

  std::shared_ptr<T> acquire() {
    assert(!empty());
    return *std::find_if(pool_.begin(), pool.end(), [](const std::shared_ptr<T>& i){return i.count() == 1;});
  }

  bool empty() const {
    return std::none_of(pool_.begin(), pool_.end(), [](const std::shared_ptr<T>& i){return i.count() == 1;});
  }

 private:
  std::vector<std::shared_ptr<T>> pool_;
};
3
  • Thanks for this suggestion. The only downside (but important one), is that acquire and empty are O(n), when they should be O(1) (since they can).
    – swalog
    Jan 8, 2015 at 9:06
  • I've added an answer with the O(1) implementation I was thinking of.
    – swalog
    Jan 8, 2015 at 10:32
  • Instantiating shared_ptr means extra memory on the heap and hence extra malloc for reference counter. Nov 12, 2021 at 5:04

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.