资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

weak_ptr和intrusive_ptr-创新互联

1、weak_ptr

创新互联公司是一家集网站建设,鹤岗企业网站建设,鹤岗品牌网站建设,网站定制,鹤岗网站建设报价,网络营销,网络优化,鹤岗网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。

 (1)、weak_ptr是为了配合shared_ptr而引入的智能指针,它更像是shared_ptr的一个助手,它不具有普通指针的行为,

没有重载operator*和->,它的大作用在于协助shared_ptr工作,像旁观者那样观测资源的使用情况。

 (2)、2个重要接口:bool expired()const ;// 判断是否过期

 lock()函数是弱指针的核心;

 (3)、获得资源的观测权,但weak_ptr没有共享资源,它的构造不会引起引用计数的增加,它的析构也不会导致引用计数减少,

它只是一个静静的观察者。

2、使用weak_ptr

 调用系统库的:

#include
#include
using namespace std;
using namespace boost;

int main(void){
    int *p = new int(10);
    shared_ptr sp(p);
    
    weak_ptr wp(sp);//1、本身对wp的创建与析构是不会增加/减少use_count;
    cout< sp1 = wp.lock();
        //3、wp的lock()函数在有效的前提下可以构造对象(构造的是所观测的对象)。
        //使use_count加1;
    }//失效的话,构建的是一个空对象,引用计数将不会在增加!!!

}

 3、weak_ptr源码剖析

 结合shared_ptr和前面的所有,删除器,shared_array、weak_ptr给出模拟的部分代码:

 模仿源代码,写出了最重要的函数:

#ifndef _CONFIG_H_
#define _CONFIG_H_

#include
using namespace std;

//#define DISPLAY

#endif
//////////////////////////////////////////////////////////////////////////////////////////
#ifndef _SHARED_PTR_H_
#define _SHARED_PTR_H_

#include"shared_count.h"

template
class weak_ptr;

template
class shared_ptr{
    friend class weak_ptr;
    typedef shared_ptr this_type;
public:
    template
        shared_ptr(Y *p, D d) : px(p), pn(p, d){}//支持传递删除器
    shared_ptr(T *p = 0) : px(p), pn(p){
#ifdef DISPLAY
        cout<<"Create shared_ptr object!"< const &r) : px(r.px), pn(r.pn){}
    shared_ptr& operator=(shared_ptr const &r){
        if(this != &r){
            this_type(r).swap(*this);//调用拷贝构造,先创建一个无名临时的对象
        }
        return *this;
    }
    ~shared_ptr(){
#ifdef DISPLAY
        cout<<"Free shared_ptr object"<()const{
        return get();
    }
    T* get()const{
        return px;
    }
public:
    long use_count()const{
        return pn.use_count();
    }
    bool unique()const{
        return pn.unique();
    }
    void reset(T *p){
        this_type(p).swap(*this);
    }
    void swap(shared_ptr &other){
        std::swap(px, other.px); //指针的交换
        pn.swap(other.pn);
    }
public:
    template
    shared_ptr(weak_ptr const &r) : pn(r.pn){
        px = r.px;
    }

private:
    T *px;
    shared_count pn;
};

#endif
/////////////////////////////////////////////////////////////////////////////////////////////////
#ifndef _SHARED_COUNT_H_
#define _SHARED_COUNT_H_

#include"config.h"
#include"sp_counted_base.h"
#include"sp_counted_impl_xx.h"


class shared_count{
    friend class weak_count;
public:
    template  //此时类型不定,写模板函数
        shared_count(T *p) : pi(new sp_counted_impl_xx(p)){
#ifdef DISPLAY
        cout<<"Create shared_cout object!"<
    shared_count(Y *p, D d) : pi(0){
        typedef Y* P;
        pi = new sp_counted_impl_pd(p, d);
    }
    shared_count(shared_count const &r) : pi(r.pi){
        if(pi){
            pi->add_ref_copy();
        }
    }
    ~shared_count(){
#ifdef DISPLAY
        cout<<"Free shared_count object"<release();
        }
    }
public:
    long use_count()const{
        return pi != 0 ? pi->use_count() : 0;
    }
    bool unique()const{
        return use_count() == 1;
    }
    void swap(shared_count &r){
        sp_counted_base *tmp = r.pi;
        r.pi = pi;
        pi = tmp;
    }
public:
    explicit shared_count(weak_count const &r);
private:
    sp_counted_base *pi;
};
/////////////////////////////////////////////////////
template
class sp_counted_impl_pd : public sp_counted_base{
public:
    sp_counted_impl_pd(P p, D d) : ptr(p), del(d){}
public:
    void dispose(){
        del(ptr);
    }
private:
    P ptr;
    D del;
};
/////////////////////////////////////////////////////////////////////
class weak_count{
    friend class shared_count;
public:
    weak_count(shared_count const &r) : pi(r.pi){
        if(pi != 0){
            pi->weak_add_ref();
        }
    }
    ~weak_count(){
        if(pi){
            pi->weak_release();
        }
    }
public:
    long use_count()const{
        return pi != 0 ? pi->use_count() : 0;
    }
private:
    sp_counted_base *pi;
};

shared_count::shared_count(weak_count const &r) : pi(r.pi){
    if(pi){
        pi->add_ref_lock();
    }
}
#endif
//////////////////////////////////////////////////////////////////////////////
#ifndef SP_COUNTED_BASE_H_
#define SP_COUNTED_BASE_H_

#include"config.h"

class sp_counted_base{  //抽象类
public:
    sp_counted_base() : use_count_(1), weak_count_(1){
#ifdef DISPLAY
        cout<<"Create sp_counted_base object"<
class shared_array{
public:
    typedef checked_array_deleter deleter;
    shared_array(T *p = 0) : px(p), pn(p, deleter()){} //无名对象
    ~shared_array(){
        
    }
public:
    T& operator[](int i)const{
        return px[i];
    }
private:
    T *px;
    shared_count pn;  //必须用到引用计数器对象
};

#endif
////////////////////////////////////////////////////////////////////////////////////////////
#ifndef _CHECKED_DELETE_H_
#define _CHECKED_DELETE_H_

template
void checked_array_delete(T *x){
    delete []x;
}

template
struct checked_array_deleter{
public:
    void operator()(T *x)const{
        checked_array_delete(x);        
    }
};

#endif
//////////////////////////////////////////////////////////////////////////////////////////////
#ifndef _WEAK_PTR_H_
#define _WEAK_PTR_H_

#include"shared_ptr.h"

template
class shared_ptr;

class shared_count;
template
class weak_ptr{
    friend class shared_ptr;
    friend class shared_count;
public:
    template
    weak_ptr(shared_ptr const &r) : px(r.px), pn(r.pn){}
    ~weak_ptr(){}
public:
    long use_count()const{
        pn.use_count();
    }
    bool expired()const{
        return pn.use_count() == 0;
    }
    shared_ptr lock()const{
        return shared_ptr(*this);
    }
private:
    T *px;
    weak_count pn;
};

#endif
///////////////////////////////////////////////////////////////////////////////////////////////////
#ifndef SP_COUNTED_IMPL_XX_H_
#define SP_COUNTED_IMPL_XX_H_

#include"sp_counted_base.h"

template
class sp_counted_impl_xx : public sp_counted_base{
public:
    sp_counted_impl_xx(T *p) : px_(p){
#ifdef DISPLAY
        cout<<"Create sp_counted_impl_xx object"<
#include"shared_ptr.h"
#include"shared_array.h"
#include"weak_ptr.h"
using namespace std;

int main(void){
    int *p = new int(10);

    shared_ptr sp(p);
    weak_ptr wp(sp);
    if(!wp.expired()){
        shared_ptr sp1 = wp.lock();//返回真实的对象
        cout<

这是这个智能指针的最主要的剖析,关键理清脉络,条理清晰一些,就可以分析出来!

4、intrusive_ptr的使用

 适用情景:(1)、对内存的占用要求非常严格,不能有引用计数的内存开销;

 (2)、这时的代码中已经有了引用计数机制管理的对象;

重点掌握如何使用:

 (1)、两个方法必须重写(增加/减少引用计数)

 (2)、必须自己编写管理引用计数的类

 (3)、要封装到一个类中,在继承(把自己的类侵入到智能指针中进行管理)

具体使用的一个类子如下:

#include
#include
using namespace std;
using namespace boost;
 
//1、实现2个重要的函数
template
void intrusive_ptr_add_ref(T *t){ //增加引用计数
    t->add_ref_copy();
}
template
void intrusive_ptr_release(T *t){  //减少引用计数
    t->release();
}

//2、提供管理对象
class Counter{
public:
    Counter() : use_count_(0){
    
    }
    ~Counter(){
    
    }
public:
    void add_ref_copy(){
        ++use_count_;
    }
    void release(){
        if(--use_count_ == 0){
            delete this;
        }
    }
private:
    long use_count_;
};
//3、定义自己的对象
class Test;
ostream& operator<<(ostream &out, const Test &t);
class Test : public Counter{
    friend     ostream& operator<<(ostream &out, const Test &t);
public:
    Test(int d = 0) : data(d){}
    ~Test(){}
private:
    int data;
};

ostream& operator<<(ostream &out, const Test &t){
    out< ip(pt);
    cout<<*ip<

结果如下:

weak_ptr和intrusive_ptr

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


文章题目:weak_ptr和intrusive_ptr-创新互联
本文URL:http://cdkjz.cn/article/dchdhi.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220