侵入式智能指针的引用计数器
Reference counter for intrusive smart pointers
以下是一位不再与我们合作的同事编写的代码摘录。我有疑问为什么以这种方式而不是任何其他方式编写此代码。我对这位同事的印象非常好,他是少数几个能够在脑海中编译C++代码的人之一。
#include <boost/smart_ptr.hpp>
namespace boost
{
/*****************************************
class RefCounter
{
protected:
RefCounter() : m_refCount(0) {}
virtual ~RefCounter() { }
private:
friend void intrusive_ptr_release(RefCounter * p);
friend void intrusive_ptr_add_ref(RefCounter * p);
volatile long m_refCount;
virtual void incRef() {__sync_add_and_fetch(&m_refCount, 1);}
virtual void decRef() {if (__sync_sub_and_fetch(&m_refCount, 1) == 0) delete this;}
};
inline void intrusive_ptr_add_ref(RefCounter * p)
{
p->incRef();
}
inline void intrusive_ptr_release(RefCounter * p)
{
p->decRef();
}
************************************************/
class RefCounter
{
protected:
RefCounter() : m_refCount(0) {}
RefCounter(const RefCounter&) : m_refCount(0) {}
virtual ~RefCounter() {}
long getRefCount() const {return m_refCount;}
private:
friend void intrusive_ptr_release(RefCounter * p);
friend void intrusive_ptr_add_ref(RefCounter * p);
volatile long m_refCount;
};
inline void intrusive_ptr_add_ref(RefCounter * p)
{
__sync_add_and_fetch(&p->m_refCount, 1);
}
inline void intrusive_ptr_release(RefCounter * p)
{
if (__sync_sub_and_fetch(&p->m_refCount, 1) == 0) delete p;
}
} // namespace boost
使用示例:
struct Foo : public boost::RefCounter
{
int a;
};
typedef boost::intrusive_ptr<Foo> FooIntrusivePtr;
FooIntrusivePtr fooPtr;
我的问题如下:
为什么要使用
friend
关键字?为什么不将函数声明为公共函数?为什么要使用
volative
关键字?编译器可以在何处以及为什么积极优化此值?或者这个关键字只是作为一种预防措施?
作为奖励,为什么以前的方法(注释掉)被现有代码替换?以前的方法是否有问题,错误或特殊情况无法按预期工作?
非常感谢。
函数被标记为friend
,以便它们可以编辑类的m_refCount
成员变量,即使它是私有的并且函数不是类本身的一部分。
volatile
关键字以及它使用 __sync_
函数的原因是允许此实现在多线程环境中工作,而无需使用互斥锁。
相关文章:
- 将对象数组的引用传递给函数
- 什么时候在C++中返回常量引用是个好主意
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- 何时在引用或唯一指针上使用移动语义
- 如何在c++中使用引用实现类似python的行为
- 编译C++时未定义的引用
- Ctypes wstring通过引用传递
- 引用计数智能指针如何避免或处理引用计数器溢出?
- 实现限制为 3 个指针的引用计数器
- C++实现了带有引用计数器的sharedpointer函数
- std::lock_guard是否递增dll引用计数器?(LoadCount)
- 引用计数器实现,= 运算符重载错误
- 侵入式智能指针的引用计数器
- 通过引用将循环计数器或范围声明传递给线程有什么区别
- C++:对象的引用计数器
- shared_ptr和shared_array为引用计数器分配内存
- 为什么在引用计数器上需要内存顺序限制
- 带有引用计数器的c++结构导致内存泄漏
- shared_ptrs是否会遇到由于引用计数器原子递增/递减而导致的缓存丢失?
- 带引用计数器的对象缓存