简介: CSDN博客专家,专注Android/Linux系统,分享多mic语音方案、音视频、编解码等技术,与大家一起成长!
优质专栏:Audio工程师进阶系列【原创干货持续更新中……】🚀
人生格言: 人生从来没有捷径,只有行动才是治疗恐惧和懒惰的唯一良药.
本篇目的:探讨智能指针shared_ptr死锁问题如何解决?
#include
#include
#include
using namespace std;
int main(){
string *buf = new string("1111");
shared_ptr<string> pa(buf);
shared_ptr<string> pb(buf);
cout << "pa.use_count " << pa.use_count() << endl;
cout << "pb.use_count " << pb.use_count() << endl;
return 0;
}
使得两个shared_ptr对象托管同一个指针:
tring *buf = new string("1111");
shared_ptr
pa(buf);
shared_ptr
pb(buf);
#include
#include
#include
using namespace std;
int main(){
string *buf1 = new string("1111");
string *buf2 = new string("2222");
shared_ptr<string> pa(buf1);
shared_ptr<string> pb(buf2);
pa = pb;
cout << "pa.use_count " << pa.use_count() << endl;
cout << "pb.use_count " << pb.use_count() << endl;
return 0;
}
#include
#include
#include
using namespace std;
class Test2;
class Test1{
public:
shared_ptr<Test2> pb_;
~Test1(){
cout << "Test1 delete\n";
}
};
class Test2{
public:
shared_ptr<Test1> pa_;
~Test2(){
cout << "Test2 delete\n";
}
};
void test() {
//v1.0
// shared_ptr pb(new Test2());
// shared_ptr pa(new Test1());
// pb->pa_ = pa;
// pa->pb_ = pb;
//v2.0
shared_ptr<Test1> pa = make_shared<Test1>();
shared_ptr<Test2> pb = make_shared<Test2>();
pb->pa_ = pa;
pa->pb_ = pb;
//由于share_ptr是共享资源,所以pb所指向的资源的引用计数也会加1
cout << "pb.use_count " << pb.use_count() << endl;//2
cout << "pa.use_count " << pa.use_count() << endl;//2
}//程序结束时,没有调用Test1和Test2的析构函数
int main(){
test();
return 0;
}
1.weak_ptr是用于解决shared_ptr相互引用时产生死锁问题的智能指针。
2.两个shared_ptr相互引用,这两个shared_ptr指针的引用计数永远不会下降为0,资源永远不会释放。
3.weak_ptr是对对象的一种弱引用,它不会增加对象的use_count,weak_ptr和shared_ptr可以相互转化,
4.shared_ptr可以直接赋值给weak_ptr,weak_ptr也可以通过调用lock函数来获得shared_ptr。
5.将一个weak_ptr绑定到一个shared_ptr不会改变shared_ptr的引用计数。
6.指向对象的shared_ptr被销毁、释放。weak_ptr指向对象也会被释放。
#include
#include
#include
using namespace std;
class Test2;
class Test1{
public:
//shared_ptr pb_;
weak_ptr<Test2> pb_;
~Test1(){
cout << "Test1 delete\n";
}
};
class Test2{
public:
shared_ptr<Test1> pa_;
~Test2(){
cout << "Test2 delete\n";
}
};
void test() {
//v1.0
// shared_ptr pb(new Test2());
// shared_ptr pa(new Test1());
// pb->pa_ = pa;
// pa->pb_ = pb;
//v2.0
shared_ptr<Test1> pa = make_shared<Test1>();
shared_ptr<Test2> pb = make_shared<Test2>();
pb->pa_ = pa;
pa->pb_ = pb;
cout << "pb.use_count " << pb.use_count() << endl;//2
cout << "pa.use_count " << pa.use_count() << endl;//2
}
int main(){
test();
return 0;
}
将shared_ptr改为weak_ptr弱智能指针。