【Boost】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函數來提供引用計數功能。
/** * 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<Tconst*>(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>((Tconst*)this);}int refcount()const {return ref_count;}private:///should be modifiable even from const intrusive_ptr objectsmutable boost::detail::atomic_count ref_count;};
用戶類類型需要繼承intrusive_ptr_base基類,以便具有引用計數功能。
#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析構時。即不能如下使用:
int *a = new int(5); boost::shared_ptr ptr1(a); boost::shared_ptr ptr2(a); //錯誤!? boost::intrusive_ptr完全具備boost::shared_ptr的功能,且不存在shared_ptr的問題,即可以利用raw pointer創建多個intrusive _ptr,其原因就在于引用計數的ref_count對象,shared_ptr是放在shared_ptr結構里,而目標對象T通過繼承intrusive_ptr_base將引用計數作為T對象的內部成員變量,就不會出現同一個對象有兩個引用計數器的情況出現。?
那么為什么通常鼓勵大家使用shared_ptr,而不是intrusive_ptr呢, 在于shared_ptr不是侵入性的,可以指向任意類型的對象; 而intrusive_ptr所要指向的對象,需要繼承intrusive_ptr_base,即使不需要,引用計數成員也會被創建。
??
結論:如果創建新類且需要進行傳遞,則繼承intrusive_ptr_base,使用intrusive_ptr。原文地址:
boost intrusive_ptr原理(兼對比shared_ptr)
boost intrusive_ptr
總結
以上是生活随笔為你收集整理的【Boost】boost库中智能指针——intrusive_ptr的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Boost】boost库中智能指针——
- 下一篇: 拦截聊天记录--Hook技巧简介【原创】