在询问是否有比 boost::object_pool 更快的堆分配/释放机制可用?我得到反馈说这个对象池不是线程安全的。
所以我写了一个 ObjectFactory 包装 boost::object_pool 并添加互斥锁:
#include <memory>
using std::shared_ptr;
#include <boost/pool/object_pool.hpp>
#include <boost/thread/mutex.hpp>
template <typename T>
class ObjectFactory
{
private:
struct SharedDeleter
{
ObjectFactory<T>* m_pFact;
SharedDeleter(ObjectFactory<T>* fact) : m_pFact(fact) {}
inline void operator()(T* p) const
{
m_pFact->destroy(p);
}
};
boost::object_pool<T> m_Pool;
boost::mutex m_PoolMutex;
SharedDeleter m_Deleter;
public:
ObjectFactory() : m_Deleter(this)
{
}
template<typename TType = T, typename... TArgs>
inline TType* create(TArgs&&... mArgs)
{
boost::unique_lock<boost::mutex> scoped_lock(m_PoolMutex);
return m_Pool.construct(mArgs...);
}
inline void destroy(T* mObj)
{
boost::unique_lock<boost::mutex> scoped_lock(m_PoolMutex);
m_Pool.destroy(mObj);
}
template<typename TType = T, typename... TArgs>
inline std::shared_ptr<TType> make_shared(TArgs&&... mArgs)
{
return std::shared_ptr<TType>(this->create(mArgs...), m_Deleter);
}
};
没有互斥锁的计时结果:
With WithObjectFactory!:
Start time: 1381682855810868000 nanoseconds since Jan 1, 1970
End time: 1381682863375427000 nanoseconds since Jan 1, 1970
Duration: 7.56456 seconds
With WithObjectFactory and std::shared_ptr!:
Start time: 1381682863375476000 nanoseconds since Jan 1, 1970
End time: 1381682879114065000 nanoseconds since Jan 1, 1970
Duration: 15.7386 seconds
使用互斥锁的计时结果:
With WithObjectFactory!:
Start time: 1381683562246086000 nanoseconds since Jan 1, 1970
End time: 1381683574399319000 nanoseconds since Jan 1, 1970
Duration: 12.1532 seconds
With WithObjectFactory and std::shared_ptr!:
Start time: 1381683574399378000 nanoseconds since Jan 1, 1970
End time: 1381683595103438000 nanoseconds since Jan 1, 1970
Duration: 20.7041 seconds
您看到的是,互斥锁占用了 20% 以上的时间,恕我直言,boost::object_pool 仅可用于单线程应用程序。
我的问题:我是否使用了正确的锁定机制?销毁是否也需要互斥锁?我在上面的代码中没有看到任何错误吗?
谢谢
编辑:用 std::map 测试但速度很慢。找到 boost::thread_specific_ptr ,它似乎工作:
#include <memory>
using std::shared_ptr;
#include <boost/pool/object_pool.hpp>
#include <boost/thread.hpp>
template <typename T>
class ObjectFactory
{
private:
struct SharedDeleter
{
ObjectFactory<T>* m_pFact;
SharedDeleter(ObjectFactory<T>* fact) : m_pFact(fact) {}
inline void operator()(T* p) const
{
m_pFact->destroy(p);
}
};
boost::thread_specific_ptr<boost::object_pool<T>> m_tpPool;
SharedDeleter m_Deleter;
public:
ObjectFactory() : m_Deleter(this)
{
if ( !m_tpPool.get() )
m_tpPool.reset(new boost::object_pool<T>());
}
template<typename TType = T, typename... TArgs>
inline TType* create(TArgs&&... mArgs)
{
return m_tpPool->construct(mArgs...);
}
inline void destroy(T* mObj)
{
m_tpPool->destroy(mObj);
}
template<typename TType = T, typename... TArgs>
inline std::shared_ptr<TType> make_shared(TArgs&&... mArgs)
{
return std::shared_ptr<TType>(this->create(mArgs...), m_Deleter);
}
};
上面代码的结果:
With WithObjectFactory!:
Start time: 1381714923605177000 nanoseconds since Jan 1, 1970
End time: 1381714934202228000 nanoseconds since Jan 1, 1970
Duration: 10.5971 seconds
With WithObjectFactory and std::shared_ptr!:
Start time: 1381714934202285000 nanoseconds since Jan 1, 1970
End time: 1381714950900537000 nanoseconds since Jan 1, 1970
Duration: 16.6983 seconds