C++:对象的引用计数器
C++: reference counter for objects
我需要一个未在堆上分配的对象的引用计数器。
我需要它来在无法轻易复制和销毁的对象上实现RAII机制:
class File
{
private:
int fd;
public:
File( const std::string &path ) ... // opening file
destroy( ); // actually closing file
File( const File &f ) ... // just copying the fd
~File( ); // doing nothing
}
对于这样的场景,通常使用std::shared_ptr
:共享指针的对象的构造函数和析构函数只调用一次。
然而,在我的情况下,我更希望避免在堆上分配对象。我需要一个做类似于std::shared_ptr
的工作的shared_object
类,这样我的类的非复制构造函数和destroy
函数(在上面的例子中)只被调用一次。
像这样的东西存在吗?
如果您想在动态存储("堆上")中分配nothing时具有共享指针行为,您可以查看各种智能指针实现策略。在《现代C++设计》中,作者在"智能指针"一章中讨论了其中的许多策略,该章可以在网上免费(合法)获得。
您感兴趣的技术是引用链接。使用这种技术,智能指针对象在双向双链表中链接在一起,而不是指向动态分配的引用计数器。
话虽如此,使用std::shared_ptr
、std::unique_ptr
或它们的Boost变体可能会更快地编写,更容易维护。如果动态分配和引用计数曾经是一个瓶颈(我怀疑它会是,但我们不能过于草率地概括),那么您总是可以花时间使用自定义引用链接版本。
您可以向std::shared_ptr提供自己的deleter,该deleter将调用您的自定义destroy函数而不是delete。
class File
{
private:
int fd;
public:
static void destroyThis(File* f){f->destroy();}
File( const std::string &path ) ... // opening file
void destroy( ); // actually closing file
File( const File &f ) ... // You probably don't need this anymore.
~File( ); // doing nothing
};
File fileObj("path");
std::shared_ptr<File> pf(&fileObj,std::bind(&File::destroyThis,std::placeholders::_1));
std::shared_ptr<File> pf2(pf);
我相信以下架构能够满足您的需求:
// pseudo-code
class File
{
private:
int fd;
File* prev;
File* next;
public:
File(const std::string &path) :
fd(open(path)),
prev(0),
next(0)
{}
void destroy()
{
close(fd);
}
File( const File &f )
fd(f.fd),
prev(&f),
next(f.next)
{
if (next)
next->prev = this;
f.next = this;
}
~File()
{
if (prev)
prev->next = next;
if (next)
next->prev = prev;
if ((!prev) && (!next))
destroy();
}
};
在重复的文件实例之间维护一个双链接列表。列表的最后一个成员,因此最后一个重复调用destroy。不需要堆分配。
(显然这不是线程安全的。你可以用互斥来保护,也可以用无锁的方法来维护列表。)
相关文章:
- 将对象数组的引用传递给函数
- 什么时候在C++中返回常量引用是个好主意
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- 何时在引用或唯一指针上使用移动语义
- 如何在c++中使用引用实现类似python的行为
- 编译C++时未定义的引用
- Ctypes wstring通过引用传递
- 引用计数智能指针如何避免或处理引用计数器溢出?
- 实现限制为 3 个指针的引用计数器
- C++实现了带有引用计数器的sharedpointer函数
- std::lock_guard是否递增dll引用计数器?(LoadCount)
- 引用计数器实现,= 运算符重载错误
- 侵入式智能指针的引用计数器
- 通过引用将循环计数器或范围声明传递给线程有什么区别
- C++:对象的引用计数器
- shared_ptr和shared_array为引用计数器分配内存
- 为什么在引用计数器上需要内存顺序限制
- 带有引用计数器的c++结构导致内存泄漏
- shared_ptrs是否会遇到由于引用计数器原子递增/递减而导致的缓存丢失?
- 带引用计数器的对象缓存