- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
在询问 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
最佳答案
结果:在带有 GCC 4.6 的 Ubuntu LTS 12.04 上
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
因此线程安全 ObjectFactory 仍然比使用普通的旧的 new 和 delete 快 1 秒以上。
大家有更好的建议欢迎补充!
感谢所有有用的反馈!
编辑:使用 __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 不需要是静态的吗?
关于c++ - 如何让 boost::object_pool 线程安全?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19347890/
我想为 std::map 指定 allocator。所以写一个自定义的,从here中获取基本代码约苏蒂斯。我想使用 boost::object_pool 来获得高性能。但是boost::object_
我想创建一个对象池,但我希望它只在我的内存堆的特定段上分配内存。有没有办法使用 boost 来做到这一点? 最佳答案 Boost.Pool 的 object_pool允许用户通过提供 UserAllo
boost::object_pool 是同步的吗? 最佳答案 C++ 没有指定任何关于线程安全的内容,因此如果没有提及,它可能不涉及线程。有时,Boost 提供开箱即用的线程安全的东西,这不是其中之一
在询问 Is there a faster heap allocation/deallocation mechanism available than boost::object_pool? 后,我得
我的应用程序有一个类“MyClass”。它的对象是从 Boost Object_pool 构建的。 我需要通过 Boost 二进制序列化对此类对象进行序列化/反序列化。 用于序列化 - 我从池中取出一
我尝试使用 boost::object_pool 创建一个包含 vector 作为其成员数据的对象。这是代码。 #include #include #include class A { publ
这周我发现了 boost::object_pool 并且惊讶于它比普通的新建和删除快了大约 20-30%。 为了测试,我编写了一个小型 C++ 应用程序,它使用 boost::chrono 为不同的堆
这是一个由以下代码说明的两部分问题: #include #include #include struct Foo { Foo(int i) : _i(i) {} void* oper
我正在尝试实现一个 boost::multi_index 应用程序,但性能非常糟糕:插入 10,000 个对象几乎需要 0.1 秒,这是 Not Acceptable 。因此,当我查看文档并发现 bo
是否可以通过非 const 引用使用 boost::object_pool<>::construct? 以下代码段无法编译 (VS2010): foo::foo(bar & b) { } static
我是一名优秀的程序员,十分优秀!