如何获得boost :: object_pool线程安全?

时间:2013-10-13 17:05:54

标签: c++ multithreading memory-management boost

在询问Is there a faster heap allocation/deallocation mechanism available than 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只能用于单线程应用程序。

我的问题: 我使用正确的锁定机制吗? destroy还需要一个互斥锁吗? 我在上面的代码中没有看到任何错误吗?

由于

编辑: 用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

2 个答案:

答案 0 :(得分:1)

  • 使用std :: mutex和boost:thread_specific_ptr,两者都减慢
  • std :: atomic_load(std :: shared_ptr&lt; ...&gt; *)似乎不适用于GCC 4.6
  • 使用this解决方案
  • 回退到旧的pthread调用

结果:在Ubuntu LTS 12.04上使用GCC 4.6

 With OUT smartptrs (new and delete): 
Start time: 1381746876399819258 nanoseconds since Jan 1, 1970
End time: 1381746881851990579 nanoseconds since Jan 1, 1970
Duration: 5.45217 seconds

 With smartptrs (boost::shared_ptr withOUT make_shared): 
Start time: 1381746881852079492 nanoseconds since Jan 1, 1970
End time: 1381746889453586405 nanoseconds since Jan 1, 1970
Duration: 7.60151 seconds

 With smartptrs (boost::shared_ptr with make_shared): 
Start time: 1381746889453642790 nanoseconds since Jan 1, 1970
End time: 1381746896396534068 nanoseconds since Jan 1, 1970
Duration: 6.94289 seconds

 With STD smart_ptr (std::shared_ptr with make_shared): 
Start time: 1381746896396596314 nanoseconds since Jan 1, 1970
End time: 1381746902544346880 nanoseconds since Jan 1, 1970
Duration: 6.14775 seconds

 With UniquePtr (boost::unique_ptr): 
Start time: 1381746902544386766 nanoseconds since Jan 1, 1970
End time: 1381746907842640751 nanoseconds since Jan 1, 1970
Duration: 5.29825 seconds

 With STD UniquePtr (std::unique_ptr): 
Start time: 1381746907842679994 nanoseconds since Jan 1, 1970
End time: 1381746913141429138 nanoseconds since Jan 1, 1970
Duration: 5.29875 seconds

 With Object Pool (boost::object_pool<>): 
Start time: 1381746913141469017 nanoseconds since Jan 1, 1970
End time: 1381746917062689541 nanoseconds since Jan 1, 1970
Duration: 3.92122 seconds


 With Thread Safe ObjectFactory<TestClass>...
Start time: 1381746917062729671 nanoseconds since Jan 1, 1970
End time: 1381746921388452186 nanoseconds since Jan 1, 1970
Duration: 4.32572 seconds

 With Thread Safe ObjectFactory<TestClass> and std::shared_ptr...
Start time: 1381746921388491395 nanoseconds since Jan 1, 1970
End time: 1381746928808481617 nanoseconds since Jan 1, 1970
Duration: 7.41999 seconds

因此,Thread Safe ObjectFactory比使用普通的旧版&amp;删除。

任何人都有建议,请添加它!

感谢所有有用的反馈!

编辑: 使用__thread gnu选项也很快:

#include <memory>
#include <boost/pool/object_pool.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);
        }
    };

    static __thread boost::object_pool<T>* m_tlsPool;
    SharedDeleter           m_Deleter;

public:
    ObjectFactory() :  m_Deleter(this)
    {
        m_tlsPool = new boost::object_pool<T>();
    }

    virtual ~ObjectFactory() 
    {
        delete m_tlsPool;
    }

    template<typename TType = T, typename... TArgs> 
    inline TType* create(TArgs&&... mArgs)
    {
        return m_tlsPool->construct(mArgs...);       
    }

    inline void destroy(T* mObj) 
    { 
        m_tlsPool->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);
    }
};

template<typename T>
/*static*/__thread boost::object_pool<T>* ObjectFactory<T>::m_tlsPool(NULL);

但是m_tlsPool不需要是静态的吗?

答案 1 :(得分:0)

我认为你仍然需要m_Deleter的同步,因为shared_ptr可以从另一个线程释放。