我希望对象由shared_ptr管理,且从池中分配,比如Boost的Pool接口,如何实现?
这是你所需的代码(可能无法编译,因为我手头没有 boost 并且我是从记忆中编写的):
class YourClass; // your data type, defined somewhere else
boost::object_pool<YourClass> allocator;
void destroy(YourClass* pointer)
{
allocator.destroy(pointer);
}
boost::shared_ptr<YourClass> create()
{
// usage of object_pool<??>::construct requires that you have a
// YourClass::YourClass(void) defined. If you need to pass arguments
// to the new instance, you need to do that separately.
//
// for example using a YourClass::Initialize(your,parameters,here) method
// before returning from this function
return boost::shared_ptr<YourClass>( allocator.construct(), &destroy );
}
// usage:
boost::shared_ptr<YourClass> newObject = create();
我在两个不同的项目中都实现了这个功能,它们都是在一个工厂类的成员函数中实现(可以在使用分配器的时候加上boost::mutex
锁来保证线程同步),同时destroy
函数的签名也因为使用了boost::bind
而修改为void (YourClass*)
。
你还可以通过在boost::shared_ptr
构造函数中直接绑定object_pool<YourClass>::destroy
来避免写两个额外的函数(destroy
和create
)。
我现在懒得写那么多代码了 :)
编辑(将我的答复评论移动到这里方便代码格式化):
绑定destroy
函数:
class ClassFactory
{
boost::object_pool<YourClass> allocator;
public:
boost::shared_ptr<YourClass> create()
{
return boost::shared_ptr<YourClass>(
allocator.construct(),
boost::bind(&ClassFactory::destroy, this, _1) );
}
void destroy(YourClass* pointer)
{
allocator.destroy(pointer);
}
};
ClassFactory
的生命周期应该比 shared_ptr
更长(如果删除 ClassFactory
实例,则传递给 shared_ptr
实例的 this 指针将无效,并且在 shared_ptr
删除 YourClass
实例时会导致您的应用程序崩溃)。
boost::pool_alloc
与boost::allocate_shared
一起使用? - dvide这些几乎是正交的问题。shared_ptr
在对象的分配中没有任何作用。
它所涉及的是不再被引用的内存的删除。如果您从除默认堆之外的任何地方进行了分配,则需要提供自定义删除器。
template<class Y, class D, class A> shared_ptr(Y * p, D d, A a);
问题是,如果我要使用 Boost 的 pool_alloc,D 参数该如何处理? - myahya
construct
不接受任何参数的技术原因吗? - Stephan Dollberg