shared_ptr


shared_ptr模板类有一个__shared_count类型的成员_M_refcount来处理引用计数的问题。__shared_count也是一个模板类,它的内部有一个指向Sp_counted_base_impl类型的指针_M_pi。所有引用同一个对象的shared_ptr都共用一个_M_pi指针。

当一个shared_ptr拷贝复制时, _M_pi指针调用_M_add_ref_copy()函数将引用计数+1。 当shared_ptr析构时,_M_pi指针调用_M_release()函数将引用计数-1。 _M_release()函数中会判断引用计数是否为0. 如果引用计数为0, 则将shared_ptr引用的对象内存释放掉。

   __shared_count(const __shared_count& __r)
      : _M_pi(__r._M_pi) // nothrow
      {
    if (_M_pi != 0)
      _M_pi->_M_add_ref_copy();
      }


      ~__shared_count() // nothrow
      {
    if (_M_pi != 0)
      _M_pi->_M_release();
      }

   _M_pi = new _Sp_counted_base_impl<_Ptr, _Deleter, _Lp>(__p, __d);

      void _M_release() // nothrow
      {
          if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, -1) == 1)
          {
              _M_dispose();
              // There must be a memory barrier between dispose() and destroy()
              // to ensure that the effects of dispose() are observed in the
              // thread that runs destroy().
              // See http://gcc.gnu.org/ml/libstdc++/2005-11/msg00136.html
              if (_Mutex_base<_Lp>::_S_need_barriers)
              {
                  _GLIBCXX_READ_MEM_BARRIER;
                  _GLIBCXX_WRITE_MEM_BARRIER;
              }
              if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count,
                          -1) == 1)
                  _M_destroy();
          }
      }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值