shared_ptr에서 반환되는 동시 객체 풀을 구현하고이를 명시 적으로 풀로 반환하는 것이 필요하지 않았습니다. 나는 기본적으로 배열을 shared_ptrs를 동시 대기열에 넣고 할당하고 사용자 정의 삭제자를 구현했습니다. 일하는 것 같습니다. 내가 놓친 게 있니?boost :: shared_ptr을 제공하는 동시 객체 풀
#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 ConcurrentObjectPoolDeletor
} // Namespace COP
#endif // CONCURRENTOBJECTPOOL_H
이것은 http://codereview.stackexchange.com/에 속합니다. –
헤더의 익명 네임 스페이스가 나에게 나쁜 생각 인 것 같습니다. ODR 위반에 대한 빠른 방법. –
[Boost.Flyweight] (http://www.boost.org/doc/libs/release/libs/flyweight/doc/index.html)가 이미 문제를 해결합니까? – ildjarn