日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【Boost】boost库中智能指针——intrusive_ptr

發(fā)布時(shí)間:2024/4/11 编程问答 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Boost】boost库中智能指针——intrusive_ptr 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

boost::intrusive_ptr一種“侵入式”的引用計(jì)數(shù)指針,它實(shí)際并不提供引用計(jì)數(shù)功能,而是要求被存儲(chǔ)的對(duì)象自己實(shí)現(xiàn)引用計(jì)數(shù)功能,并提供intrusive_ptr_add_refintrusive_ptr_release函數(shù)接口供boost::intrusive_ptr調(diào)用。

下面通過一個(gè)具體的例子來說明boost::intrusive_ptr的用法,首先實(shí)現(xiàn)一個(gè)基類intrusive_ptr_base,定義intrusive_ptr_add_ref和intrusive_ptr_release函數(shù)來提供引用計(jì)數(shù)功能。

/** * intrusive_ptr_base基類,提供intrusive_ptr_add_ref()和intrusive_ptr_release()函數(shù)來提供引用計(jì)數(shù)功能; * 使用boost::intrusive_ptr指針存儲(chǔ)的用戶類類型必須繼承自intrusive_ptr_base基類。 */ #include <ostream> #include <boost/checked_delete.hpp> #include <boost/detail/atomic_count.hpp> template<class T> class intrusive_ptr_base { public:/*** 缺省構(gòu)造函數(shù)*/intrusive_ptr_base(): ref_count(0) {std::cout << " Default constructor " << std::endl;}/*** 不允許拷貝構(gòu)造,只能使用intrusive_ptr來構(gòu)造另一個(gè)intrusive_ptr*/intrusive_ptr_base(intrusive_ptr_base<T>const&): ref_count(0) {std::cout << " Copy constructor..." << std::endl;}/*** 不允許進(jìn)行賦值操作*/intrusive_ptr_base& operator=(intrusive_ptr_base const& rhs) {std::cout << " Assignment operator..." << std::endl;return *this;}/*** 遞增引用計(jì)數(shù)(放到基類中以便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;}/*** 遞減引用計(jì)數(shù)*/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的實(shí)際類型就是T,intrusive_ptr_base<T>為基類}/*** 類似于shared_from_this()函數(shù)*/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基類,以便具有引用計(jì)數(shù)功能。

#include <iostream> #include <string> #include <boost/intrusive_ptr.hpp> #include "intrusive_ptr_base.hpp"/** * 用戶類類型繼承自intrusive_ptr_base,該實(shí)現(xiàn)方式類似于boost::enable_shared_from_this<Y> */ class Connection : public intrusive_ptr_base< Connection > { public:/*** 構(gòu)造函數(shù),調(diào)用intrusive_ptr_base< Connection >的缺省構(gòu)造函數(shù)來初始化對(duì)象的基類部分*/Connection(int id, std::string tag):connection_id( id ), connection_tag( tag ) {}/*** 拷貝構(gòu)造函數(shù),只復(fù)制自身數(shù)據(jù),不能復(fù)制引用計(jì)數(shù)部分*/Connection(const Connection& rhs):connection_id( rhs.connection_id ), connection_tag( rhs.connection_tag) {}/*** 賦值操作,同樣不能復(fù)制引用計(jì)數(shù)部分*/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") ); //調(diào)用intrusive_ptr_add_ref()遞增引用計(jì)數(shù)std::cout << "Create an intrusive ptr. Refcount = " << con0->refcount() << std::endl;boost::intrusive_ptr< Connection > con1 (con0); //調(diào)用intrusive_ptr_add_ref()std::cout << "Create an intrusive ptr. Refcount = " << con1->refcount() << std::endl;boost::intrusive_ptr< Connection > con2 = con0; //調(diào)用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; }

程序運(yùn)行輸出:

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...

?


對(duì)比boost::shared_ptr

使用boost::shared_ptr用戶類本省不需要具有引用計(jì)數(shù)功能,而是由boost::shared_ptr來提供;使用boost::shared_ptr的一大陷阱就是用一個(gè)raw pointer多次創(chuàng)建boost::shared_ptr,這將導(dǎo)致該raw pointer被多次銷毀當(dāng)boost::shared_ptr析構(gòu)時(shí)。即不能如下使用:

int *a = new int(5); boost::shared_ptr ptr1(a); boost::shared_ptr ptr2(a);  //錯(cuò)誤!? boost::intrusive_ptr完全具備boost::shared_ptr的功能,且不存在shared_ptr的問題,即可以利用raw pointer創(chuàng)建多個(gè)intrusive _ptr,其原因就在于引用計(jì)數(shù)的ref_count對(duì)象,shared_ptr是放在shared_ptr結(jié)構(gòu)里,而目標(biāo)對(duì)象T通過繼承intrusive_ptr_base將引用計(jì)數(shù)作為T對(duì)象的內(nèi)部成員變量,就不會(huì)出現(xiàn)同一個(gè)對(duì)象有兩個(gè)引用計(jì)數(shù)器的情況出現(xiàn)。

?

那么為什么通常鼓勵(lì)大家使用shared_ptr,而不是intrusive_ptr呢, 在于shared_ptr不是侵入性的,可以指向任意類型的對(duì)象; 而intrusive_ptr所要指向的對(duì)象,需要繼承intrusive_ptr_base,即使不需要,引用計(jì)數(shù)成員也會(huì)被創(chuàng)建。

??

結(jié)論:如果創(chuàng)建新類且需要進(jìn)行傳遞,則繼承intrusive_ptr_base,使用intrusive_ptr。

原文地址:

boost intrusive_ptr原理(兼對(duì)比shared_ptr)

boost intrusive_ptr

總結(jié)

以上是生活随笔為你收集整理的【Boost】boost库中智能指针——intrusive_ptr的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。