boost::intrusive_ptr原理介绍
boost::intrusive_ptr一种“侵入式”的引用计数指针,它实际并不提供引用计数功能,而是要求被存储的对象自己实现引用计数功能,并提供intrusive_ptr_add_ref和intrusive_ptr_release函数接口供boost::intrusive_ptr调用。
下面通过一个具体的例子来说明boost::intrusive_ptr的用法,首先实现一个基类intrusive_ptr_base,定义intrusive_ptr_add_ref和intrusive_ptr_release函数来提供引用计数功能。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
/*** intrusive_ptr_base基类,提供intrusive_ptr_add_ref()和intrusive_ptr_release()函数来提供引用计数功能;* 使用boost::intrusive_ptr指针存储的用户类类型必须继承自intrusive_ptr_base基类。*/#include <ostream>#include <boost/checked_delete.hpp>#include <boost/detail/atomic_count.hpp>template<class T>class intrusive_ptr_base {public: /** * 缺省构造函数 */ intrusive_ptr_base(): ref_count(0) { std::cout << " Default constructor " << std::endl; } /** * 不允许拷贝构造,只能使用intrusive_ptr来构造另一个intrusive_ptr */ intrusive_ptr_base(intrusive_ptr_base<T> const&): ref_count(0) { std::cout << " Copy constructor..." << std::endl; } /** * 不允许进行赋值操作 */ intrusive_ptr_base& operator=(intrusive_ptr_base const& rhs) { std::cout << " Assignment operator..." << std::endl; return *this; } /** * 递增引用计数(放到基类中以便compiler能找到,否则需要放到boost名字空间中) */ friend void intrusive_ptr_add_ref(intrusive_ptr_base<T> const* s) { std::cout << " intrusive_ptr_add_ref..." << std::endl; assert(s->ref_count >= 0); assert(s != 0); ++s->ref_count; } /** * 递减引用计数 */ friend void intrusive_ptr_release(intrusive_ptr_base<T> const* s) { std::cout << " intrusive_ptr_release..." << std::endl; assert(s->ref_count > 0); assert(s != 0); if (--s->ref_count == 0) boost::checked_delete(static_cast<T const*>(s)); //s的实际类型就是T,intrusive_ptr_base<T>为基类 } /** * 类似于shared_from_this()函数 */ boost::intrusive_ptr<T> self() { return boost::intrusive_ptr<T>((T*)this); } boost::intrusive_ptr<const T> self() const { return boost::intrusive_ptr<const T>((T const*)this); } int refcount() const { return ref_count; } private: ///should be modifiable even from const intrusive_ptr objects mutable boost::detail::atomic_count ref_count;}; |
用户类类型需要继承intrusive_ptr_base基类,以便具有引用计数功能。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
#include <iostream>#include <string>#include <boost/intrusive_ptr.hpp>#include "intrusive_ptr_base.hpp"/*** 用户类类型继承自intrusive_ptr_base,该实现方式类似于boost::enable_shared_from_this<Y>*/class Connection : public intrusive_ptr_base< Connection > {public: /** * 构造函数,调用intrusive_ptr_base< Connection >的缺省构造函数来初始化对象的基类部分 */ Connection(int id, std::string tag): connection_id( id ), connection_tag( tag ) {} /** * 拷贝构造函数,只复制自身数据,不能复制引用计数部分 */ Connection(const Connection& rhs): connection_id( rhs.connection_id ), connection_tag( rhs.connection_tag) {} /** * 赋值操作,同样不能复制引用计数部分 */ const Connection operator=( const Connection& rhs) { if (this != &rhs) { connection_id = rhs.connection_id; connection_tag = rhs.connection_tag; } return *this; }private: int connection_id; std::string connection_tag;};int main() { std::cout << "Create an intrusive ptr" << std::endl; boost::intrusive_ptr< Connection > con0 (new Connection(4, "sss") ); //调用intrusive_ptr_add_ref()递增引用计数 std::cout << "Create an intrusive ptr. Refcount = " << con0->refcount() << std::endl; boost::intrusive_ptr< Connection > con1 (con0); //调用intrusive_ptr_add_ref() std::cout << "Create an intrusive ptr. Refcount = " << con1->refcount() << std::endl; boost::intrusive_ptr< Connection > con2 = con0; //调用intrusive_ptr_add_ref() std::cout << "Create an intrusive ptr. Refcount = " << con2->refcount() << std::endl; std::cout << "Destroy an intrusive ptr" << std::endl; return 0;} |
程序运行输出:
Create an intrusive ptr
Default constructor
intrusive_ptr_add_ref...
Create an intrusive ptr. Refcount = 1
intrusive_ptr_add_ref...
Create an intrusive ptr. Refcount = 2
intrusive_ptr_add_ref...
Create an intrusive ptr. Refcount = 3
Destroy an intrusive ptr
intrusive_ptr_release...
intrusive_ptr_release...
intrusive_ptr_release...
对比boost::shared_ptr
使用boost::shared_ptr用户类本省不需要具有引用计数功能,而是由boost::shared_ptr来提供;使用boost::shared_ptr的一大陷阱就是用一个raw pointer多次创建boost::shared_ptr,这将导致该raw pointer被多次销毁当boost::shared_ptr析构时。即不能如下使用:
那么为什么通常鼓励大家使用shared_ptr,而不是intrusive_ptr呢, 在于shared_ptr不是侵入性的,可以指向任意类型的对象; 而intrusive_ptr所要指向的对象,需要继承intrusive_ptr_base,即使不需要,引用计数成员也会被创建。
boost::intrusive_ptr原理介绍的更多相关文章
- 03 Yarn 原理介绍
Yarn 原理介绍 大纲: Hadoop 架构介绍 YARN 产生的背景 YARN 基础架构及原理 Hadoop的1.X架构的介绍 在1.x中的NameNodes只可能有一个,虽然可以通过Se ...
- 04 MapReduce原理介绍
大数据实战(上) # MapReduce原理介绍 大纲: * Mapreduce介绍 * MapReduce2运行原理 * shuffle及排序 定义 * Mapreduce 最早是由googl ...
- Android Animation学习(一) Property Animation原理介绍和API简介
Android Animation学习(一) Property Animation介绍 Android Animation Android framework提供了两种动画系统: property a ...
- [转]MySQL主从复制原理介绍
MySQL主从复制原理介绍 一.复制的原理 MySQL 复制基于主服务器在二进制日志中跟踪所有对数据库的更改(更新.删除等等).每个从服务器从主服务器接收主服务器已经记录到其二进制日志的保存的更新,以 ...
- 分布式文件系统FastDFS原理介绍
在生产中我们一般希望文件系统能帮我们解决以下问题,如:1.超大数据存储:2.数据高可用(冗余备份):3.读/写高性能:4.海量数据计算.最好还得支持多平台多语言,支持高并发. 由于单台服务器无法满足以 ...
- 内存分析_.Net内存原理介绍
内存原理介绍 1. .Net应用程序中的内存 1.1.Net内存类型 Windows使用一个系统:虚拟寻址系统.这个系统的作用是将程序可用的内存地址映射到硬件内存中的实际地址上.其实际结果 ...
- 液晶常用接口“LVDS、TTL、RSDS、TMDS”技术原理介绍
液晶常用接口“LVDS.TTL.RSDS.TMDS”技术原理介绍 1:Lvds Low-Voltage Differential Signaling 低压差分信号 1994年由美国国家半导体公司提出之 ...
- 淘宝JAVA中间件Diamond详解(2)-原理介绍
淘宝JAVA中间件Diamond详解(二)---原理介绍 大家好,通过第一篇的快速使用,大家已经对diamond有了一个基本的了解.本次为大家带来的是diamond核心原理的介绍,主要包括server ...
- Traceroute原理介绍
一.路由追踪 路由跟踪,就是获取从主机A到达目标主机B这个过程中所有需要经过的路由设备的转发接口IP. 二.ICMP协议 Internet控制报文协议(internet control message ...
随机推荐
- Android 开发 命名规范(基础回顾)
标识符命名法标识符命名法最要有四种: 1 驼峰(Camel)命名法:又称小驼峰命名法,除首单词外,其余所有单词的第一个字母大写. 2 帕斯卡(pascal)命名法:又称大驼峰命名法,所有单词的第一个字 ...
- spring-boot 应用配置文件(.properties或.yml)
1.应用配置文件(.properties或.yml) .properties在配置文件中直接写: name=Isea533 server.port=8080 .yml格式的配置文件如: name: I ...
- Amoeba+Mysql 实现读写分离
About Amoeba Amoeba可译为阿米巴.变型虫Amoeba是一个开源项目,致力于Mysq的分布式数据库前端代理层Amoeba是一个以MySQL为底层数据存储,并对应用提供MySQL协议接口 ...
- 6.006 Introduction to Algorithms
课程信息 6.006 Introduction to Algorithms
- Golang教程:循环语句
循环语句用于重复执行一段代码. for 语句是 Go 中唯一的循环语句.Go 没有提供其他语言(如 C)中的 while 和 do while 语句. for 语句语法 for 语句的语法如下: fo ...
- C#实体对象序列化成Json,格式化,并让字段的首字母小写
解决办法有两种:第一种:使用对象的字段属性设置JsonProperty来实现(不推荐,因为需要手动的修改每个字段的属性) public class UserInfo { [JsonProperty(& ...
- JavaScript使用Object.defineProperty方法实现双数据绑定
Object.defineProperty这个方法非常值得学习,很多mvc框架中的双向数据绑定就是通过它来实现的. 本着互联网分享精神,今天我就将我自己的见解分享给大家,希望能有所帮助. 开始使用 O ...
- JAVA异常与异常处理详解
一.异常简介 什么是异常? 异常就是有异于常态,和正常情况不一样,有错误出错.在java中,阻止当前方法或作用域的情况,称之为异常. java中异常的体系是怎么样的呢? 1.Java中的所有不正常类都 ...
- Bazaar 版本控制工具
Bazaar是一个分布式的版本控制系统,它发布在GPL许可协议之下,并可用于Windows.GNU/Linux.UNIX以及Mac OS系统.Bazaar由Canonical公司赞助,目前已服务于Sa ...
- 移动web中的幻灯片切换效果
百度或者谷歌下类似的插件有很多,原理都差不多,关键适合自己的项目,如果移动端要引入jquery这么大的插件,只能呵呵了.... 下面是工作中针对webkit内核的浏览器写的,html很简单: < ...