TheRiver | blog

You have reached the world's edge, none but devils play past here

0%

c++ shared_ptr weak_ptr

源码分析

071001.jpg

shared_ptr.png

shared_ptr第一次创建

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
std::shared_ptr<class> a = make_shard<class>();

//1
template<typename _Tp1>
explicit shared_ptr(_Tp1* __p)
: __shared_ptr<_Tp>(__p) { }

//2
template<typename _Tp1>
explicit __shared_ptr(_Tp1* __p)
: _M_ptr(__p), _M_refcount(__p)
{
__glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
static_assert( sizeof(_Tp1) > 0, "incomplete type" );
__enable_shared_from_this_helper(_M_refcount, __p, __p);
}

//3
template<typename _Ptr>
explicit
__shared_count(_Ptr __p) : _M_pi(0)
{
__try
{
_M_pi = new _Sp_counted_ptr<_Ptr, _Lp>(__p);
}
__catch(...)
{
delete __p;
__throw_exception_again;
}
}

//4
_Sp_counted_base() noexcept
: _M_use_count(1), _M_weak_count(1) { }

shared_ptr互相赋值

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
//拷贝构造和赋值操作符类似,举一个例子
//移动的就不看了,大致一样,rc和拷贝不同

std::shared_ptr<class> b;
std::shared_ptr<class> a = b;

//1
shared_ptr(const shared_ptr<_Tp1>& __r) noexcept
: __shared_ptr<_Tp>(__r) { }

//2
__shared_ptr(const __shared_ptr<_Tp1, _Lp>& __r) noexcept
: _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount)
{ }

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

//4
void
_M_add_ref_copy()
{ __gnu_cxx::__atomic_add_dispatch(&_M_use_count, 1); }

shared_ptr析构

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
//1
~__shared_ptr() = default;

~__shared_count() noexcept
{
if (_M_pi != nullptr)
_M_pi->_M_release();
}

//2
void
_M_release() noexcept
{
// Be race-detector-friendly. For more info see bits/c++config.
_GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_use_count);
if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, -1) == 1)
{
_GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_use_count);
_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;
}

// Be race-detector-friendly. For more info see bits/c++config.
_GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count);
if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count,
-1) == 1)
{
_GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count);
_M_destroy();
}
}
}

//__exchange_and_add_dispatch 取出值,然后-1
//_M_use_count=1 析构模板对象的空间
//_M_weak_count=1 析构管理对象的对象

//3
virtual void
_M_dispose() noexcept
{ delete _M_ptr; }

virtual void
_M_destroy() noexcept
{ delete this; }

其他

其他函数之类的,记住就行了

循环引用

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 <memory>
using namespace std;

class B;
class A
{
public:
~A()
{
cout << "~A" << endl;
}

std::shared_ptr<B> _a;
};

class B
{
public:
~B()
{
cout << "~B" << endl;
}
std::shared_ptr<A> _b;
};

weak_ptr<A> wa;
weak_ptr<B> wb;
void func(void)
{
shared_ptr<A> la = make_shared<A>();
shared_ptr<B> lb = make_shared<B>();
wa = la;
wb = lb;

cout << "A->rc = " << la.use_count() << endl;
cout << "B->rc = " << lb.use_count() << endl;

la->_a = lb;
lb->_b = la;

cout << "A->rc = " << la.use_count() << endl;
cout << "B->rc = " << lb.use_count() << endl;
}

int main()
{
func();
cout << wa.use_count() << endl;
cout << wb.use_count() << endl;

return 0;
}

循环引用.png

A->rc = 1
B->rc = 1
A->rc = 2
B->rc = 2
1
1

由于weak_ptr不占用引用计数,用weak_ptr就不会存在此问题

总结

  • shared_ptr和weak_ptr两个类各自成员占用2个指针的大小
  • 给shared_ptr传入裸指针会导致在堆中new一片管理空间的大小
  • usecount=1会析构模板对象的空间
  • weakcount=1会析构管理对象的空间

ENDDING

071002.jpg

----------- ending -----------