Concurrent Object Pool giving a boost::shared_ptr
I wanted to implement a concurrent object pool where in a shared_ptr is returned and explicitly returning it to the pool is not required. I basically allocated an array pushed shared_ptrs for it in a concurrent queue and implemented a custom deletor. Seems to work. Am I missing anything?
#ifndef CONCURRENTOBJECTPOOL_H
#define CONCURRENTOBJECTPOOL_H
#include <boost/shared_ptr.hpp>
#include <boost/shared_array.hpp>
#include <tbb/concurrent_queue.h>
namespace COP
{
template<typename T>
class ConcurrentObjectPool;
namespace
{
template<typename T>
class ConcurrentObjectPoolDeletor
{
public:
ConcurrentObjectPoolDeletor(ConcurrentObjectPool<T>& aConcurrentObjectPool):
_concurrentObjectPool(aConcurrentObjectPool) {}
void operator()(T *p) const;
private:
ConcurrentObjectPool<T>& _concurrentObjectPool;
};
} // Anonymous namespace for ConcurrentObjectPoolDeletor
template <typename T>
class ConcurrentObjectPool
{
public:
ConcurrentObjectPool(const unsigned int aPoolSize)
: _goingDown(false),
_poolSize(aPoolSize),
_pool(new T[_poolSize]),
_ConcurrentObjectPoolDeletor(*this)
{
for(unsigned int i = 0; i < _poolSize; ++i)
{
boost::shared_ptr<T> curr(&_pool[i], _ConcurrentObjectPoolDeletor);
_objectQueue.push(curr);
}
}
boost::shared_ptr<T> loan()
{
boost::shared_ptr<T> curr;
_objectQueue.pop(curr);
return curr;
}
~ConcurrentObjectPool()
{
_goingDown = true;
_objectQueue.clear();
}
private:
void payBack(T * p)
{
if (! _goingDown)
{
boost::shared_ptr<T> curr(p, _ConcurrentObjectPoolDeletor);
_objectQueue.push(curr);
}
}
bool _goingDown;
const unsigned int _poolSize;
const boost::shared_array<T> _pool;
const ConcurrentObjectPoolDeletor<T> _ConcurrentObjectPoolDeletor;
tbb::concurrent_bounded_queue<boost::shared_ptr<T> > _objectQueue;
friend class ConcurrentObjectPoolDeletor<T>;
};
namespace
{
template<typename T>
void ConcurrentObjectPoolDeletor<T>::operator()(T *p) const
{
_concurrentObjectPool.payBack(p);
}
} // Anonymous namespace for Concurre开发者_StackOverflowntObjectPoolDeletor
} // Namespace COP
#endif // CONCURRENTOBJECTPOOL_H
There is a race between setting the _goingDown
flag in the destructor of ConcurrentObjectPool
and reading the flag in payBack()
. It can lead to memory leaks.
Actually, maybe it's better if you do not try to make the destructor safe to run concurrently with payBack()
. It's not safe anyway, starting from the fact that the _goingDown
flag is a part of the pool object and so accessing it after the pool is destroyed would cause undefined behavior - i.e. all objects must be returned to the pool before it is destroyed.
Looks good. Are you experiencing any problems using it?
精彩评论