0

I wanna design a Concurrent object pool in C++, possible prototype as:

template<std::size_t OBJ_SIZE>
class Pool {
public:
    template<typename T>
    void new(std::shared_ptr<T> & product);
    // more details...
};
  • Once object(product) is returned, its lifetime is fully controlled by the smart pointer(s) containing the object.
  • Pool may be used by multiple threads.

How do we automatically reclaim the object/memory after user is done with it?

My idea is to have a delete() method in pool, which is called in product's destructor.

Is there a better design? e.g. a way where product classes are less coupled with Pool (note it's a generic pool). Any idea is welcomed.

Clinton
  • 57
  • 1
  • 4
  • I'm not sure I understand the requirements. Are you saying that you want the lifetime of the allocated object to be controlled by the lifetime of the unrelated shared_ptr that is passed in? – Richard Hodges Nov 20 '15 at 17:54
  • @RichardHodges, the shared_ptr is used to contain the returned object, and take control of its lifetime. – Clinton Nov 20 '15 at 18:28

1 Answers1

0

If I understand your question correctly, you need to return a shared_pointer with custom deleter. Deleter will know how to delete the object using pool's deallocate() method. This is standard approach to this problem.

SergeyA
  • 61,605
  • 5
  • 78
  • 137