

侵入式智能指针 boost::intrusive_ptr
source link: https://zhiqiang.org/coding/boost-intrusive-ptr.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

侵入式智能指针 boost::intrusive_ptr
如果理解了侵入式容器,侵入式智能指针也很容易理解。传统的智能指针std::shared_ptr
使用了和数据无关的引用计数,这带来两个问题:
- 引用计数要在
shared_ptr
对象间共享,所以它只能位于堆上。这使得每次都需要重新new
一小块内存。这导致性能问题。 - 引用计数的内存区域和数据区域不一致,缓存失效导致性能问题。
- 编写代码不善,将导致同一个数据,绑定到了两个引用计数,从而导致双重删除问题。典型代码如下:
int* x = new int; std::shared_ptr<int> x1 = x; std::shared_ptr<int> x2 = x;
侵入式智能指针试图解决这些问题,方法也特别直接,那就是将引用计数直接塞进数据本身,和数据共存亡。从用户角度,这只需要继承boost::intrusive_ptr_base
基类:
struct T : public boost::intrusive_ptr_base<T> { int age; std::string name; }; T* t = new T(); // 下面这么些很安全,而且速度很快! boost::intrusive_ptr<T> x1 =t; boost::intrusive_ptr<T> x2 =t;
我们看boost::intrusive_ptr_base
的定义,其核心就是增加一个原子操作的引用计数,以及自增和自减引用计数的函数:
template<class T> class boost::intrusive_ptr_base { public: intrusive_ptr_base() : ref_count(0) {} friend void intrusive_ptr_add_ref(intrusive_ptr_base<T> const* p) { ++p->ref_count; } friend void intrusive_ptr_release(intrusive_ptr_base<T> const* p) { if (--p->ref_count == 0) { boost::checked_delete(static_cast<T const*>(s)); } } boost::intrusive_ptr<T> self() { return boost::intrusive_ptr<T>((T*)this); } private: mutable boost::detail::atomic_count ref_count; };
接下来实际的boost::intrusive_ptr
的定义就很简单了:
template<class T> class boost::intrusive_ptr { public: intrusive_ptr(T* p, bool add_ref = true) : px(p) { if (px != 0 && add_ref) { intrusive_ptr_add_ref(px); } } ~intrusive_ptr() { if (px != 0) { intrusive_ptr_release(px); } } private: T * px; }
由于解决了std::shared_ptr
的三个问题,boost::intrusive_ptr
的效率更高。但它也有缺陷。除了侵入式设计之外,最重要的是boost::intrusive_ptr
无法支持weak_ptr
,从而无法解决环形引用问题。这时因为std::shared_ptr
里的数据对象和计数器分离,可以有不同的生命周期。shared_ptr
的引用计数里有两个计数对象,从而支持weak_ptr
。
另外一个方面,std::shared_ptr
也意识到了其性能问题,内部也提供机制解决这些问题,其核心便是std::make_shared
和std::enable_shared_from_this
。
参考:C++智能指针 3 :内存布局(非侵入式、enable_shared_from_this & 侵入式)
Q. E. D.
Recommend
-
53
C++11标准中,引入了三种智能指针:其中shared_ptr、unique_ptr露面的机会非常多,大家也越来越接受使用shared_ptr、unique_ptr来取代普通指针,省去了手动管理内存的烦恼。但是,作为智能指针三胞胎之一的weak_ptr,正如其名,存在感真是...
-
12
【内容来源】第十二届中国系统架构师大会(SACC2020)- 网络直播|宜信无侵入智能业务运维实践 【主讲人】宜信资深架构师 & 智能监控平台负责人 谢知求 【演讲简介】在对信息化建设进行整合的企业中,一个具...
-
17
std::unique_ptr, or how to express explicit ownership – Vorbrodt's C++ BlogWhen we talk about std::unique_ptr we must mention the idea of explicit resource ownership as well as the concept of a resource source and sink. By wrapping a poin...
-
29
Boost智能指针——shared_ptr boost::scoped_ptr虽然简单易用,但它不能共享所有权的特性却大大限制...
-
10
Macro std::ptr::addr_of1.51.0[−]...
-
36
标准库的 std::shared_ptr 是如何实现的? 作者: 张志强 ...
-
14
侵入式容器 Boost.Intrusive 作者: 张志强 ,...
-
9
使用 std::unique_ptr 代替原始指针 作者: 张志强 ...
-
11
Is std::exception_ptr nothrow constructible and assignable? Ra...
-
13
Give a boost :: shared_ptr & lt; CustomException & gt; advertisements is there any pitfall of the following; if (someC...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK