Shared Pointer based Object Pool Implementation with fixed object count I'm implementing a object pool with shared_ptr to access with multiple thread.
Any suggestions to improve my implementation

template &lt;typename T&gt;
class Section : public std::enable_shared_from_this&lt;Section&lt;T&gt;&gt;
{
public:

template &lt;typename... E&gt;
Section(std::size_t size, E... args)
    : std::enable_shared_from_this&lt;Section&lt;T&gt;&gt;()
    , m_mem(size,T(args...))
{
    for(auto&amp; itr: m_mem)
    {
        destroy(&amp;itr);
        m_objs.push(&amp;itr);
    }
}
virtual ~Section() = default;

void isEmpty() const
{
    std::unique_lock&lt;std::mutex&gt; lk(m_lock);
    return m_objs.empty();
}
template &lt;typename... E&gt;
std::shared_ptr&lt;T&gt; create(E... args)
{
    T* obj = getObject();       
    if(obj != nullptr)
    {
        new (obj) T(args...);
    }
    return std::shared_ptr&lt;T&gt;(obj,[this](T* ptr)-&gt;void
    {
        destroy(ptr);
        std::unique_lock&lt;std::mutex&gt; lk(m_lock);
        m_objs.push(ptr);
    });
}

private:
std::list&lt;T&gt; m_mem;
std::queue&lt;T*&gt; m_objs;
mutable std::mutex m_lock;


T* getObject()
{
    std::unique_lock&lt;std::mutex&gt; lk(m_lock);
    if (!m_objs.empty())
    {
        T* obj = m_objs.front();
        m_objs.pop();
        return obj;
    }
    return nullptr;
}

void destroy(T* obj)
{
    obj-&gt;~T();
}       
};
Small nitpicks


The constructor and create could profit from perfect forwarding args (so references of all kinds can be used.

template&lt;typename... E&gt;
Section(size_t size, E&amp;&amp;... args) :
    std::enable_shared_from_this&lt;Section&lt;T&gt;&gt;(),
    m_mem(size, T(std::forward&lt;E&gt;(args)...))
    { /* ... */ }

Do you have any specific reason to prefer std::list&lt;T&gt; over std::vector&lt;T&gt; for m_mem? std::vector&lt;T&gt; would use less memory (no pointers from each element to the next), and the memory used would be contiguous (so iterating over it is very likely faster due to cache effects).


Correctness

Every std::shared_ptr&lt;T&gt; returned by Section&lt;T&gt;::create() might be dangling. This can happen if the Section&lt;T&gt; gets destroyed while there are still std::shared_ptr&lt;T&gt; pointing to contained objects.

This is a result of the std::shared_ptr&lt;T&gt; not owning the pointer.

If you can guarantee that there won't be any std::shared_ptr&lt;T&gt; pointing into the Section&lt;T&gt; when the Section&lt;T&gt; is destroyed, this problem easily disappears.

If this can't be guaranteed, there are two options:


Introducing your own smart pointer type that checks the Section&lt;T&gt; for validity every time it's dereferenced, as the standard smart pointers (std::unique_ptr, std::shared_ptr and std::weak_ptr) are not designed for this task.
(Ab-)using the returned std::shared_ptr&lt;T&gt; to keep the section alive. This can be done in the lamda init capture of the custom deleter.

return std::shared_ptr&lt;T&gt;(obj, [section = shared_from_this()](T *ptr) -&gt; void
{
    section-&gt;destroy(ptr);
    std::unique_lock&lt;std::mutex&gt; lk(section-&gt;m_lock);
    section-&gt;m_objs.push(ptr);
});


Note: This might keep the section alive far longer than intended! (Some might also say that this is an ugly hack...)