智能指针 多线程线程安全问题 删除器

template<typename T>
class RefCnt {
public:
	RefCnt(T* ptr=nullptr)
		:mptr(ptr)
		,mcount(1)
	{}
	void addRef() { mcount++; }
	int delRef() { return --mcount; }
private:
	T* mptr;
	int mcount;
};
template<typename T>
class CSmartPtr {
public:
	CSmartPtr(T* ptr=nullptr)
		:mptr(ptr)
		,pmRefCnt(new RefCnt<T>(ptr))
	{}
	CSmartPtr(const CSmartPtr<T>& src) {
		mptr = src.mptr;
		pmRefCnt = src.pmRefCnt;
		if (mptr != nullptr) {
			pmRefCnt->addRef();
		}
	}
	CSmartPtr<T>& operator=(const CSmartPtr<T>& src) {
		if (this == &src) {
			return *this;
		}
		mptr = src.mptr;
		pmRefCnt = src.pmRefCnt;
		if (mptr != nullptr) {
			pmRefCnt->addRef();
		}
		return *this;
	}
	T& operator*()const {
		return *mptr;
	}
	T* operator->()const {
		return mptr;
	}
	~CSmartPtr() {
		if (0 == pmRefCnt->delRef())
		{
			delete mptr;
			mptr = nullptr;
		}
	}
private:
	T* mptr;
	RefCnt<T>* pmRefCnt;
};
int main() {
	CSmartPtr<int> p1(new int());
	CSmartPtr<int> p2 = p1;
	CSmartPtr<int> p3;
	p3 = p2;
	cout << *p3 << endl;
	*p3 = 20;
	cout << *p3 << endl;
	return 0;
}
class A {
public:
	A(int data) {
		cout << "A()" << endl;
	}
	~A() {
		cout << "~A()" << endl;
	}
	void funcA() { cout<<"funcA()" << endl; }
private:
	int ma;
};
class B {
public:
	B(int data=0)
		:_a(data)
	{
		cout << "B()" << endl;
	}
	void funcB() { cout << "funcB()" << endl; }
private:
	A _a;
};
int main() {
	B b;
	b.funcB();
	return 0;
}
class B;
class A {
public:
	A() { cout << "A()" << endl; }
	~A() { cout << "~A()" << endl; }
	shared_ptr<B> _ptrb;
};
class B {
public:
	B() { cout << "B()" << endl; }
	~B() { cout << "~B()" << endl; }
	shared_ptr<A> _ptra;
};
int main() {
	shared_ptr<A> pa( new A());
	shared_ptr<B> pb( new B());
	//资源泄露
	pa->_ptrb = pb;
	pb->_ptra = pa;
	cout << pa.use_count() << endl;
	cout << pb.use_count() << endl;
	return 0;
}

 

class B;
class A {
public:
	A() { cout << "A()" << endl; }
	~A() { cout << "~A()" << endl; }
	void testA() { cout << "好方法" << endl; }
	weak_ptr<B> _ptrb;
};
class B {
public:
	B() { cout << "B()" << endl; }
	~B() { cout << "~B()" << endl; }
	void func() {
		shared_ptr<A> ps = _ptra.lock();//提升方法
		if (ps != nullptr) {//提升成功
			ps->testA();
		}
	}
	weak_ptr<A> _ptra;
};
int main() {
	shared_ptr<B> pb(new B());
	shared_ptr<A> pa(new A);
	pa->_ptrb = pb;
	pb->_ptra = pa;
	pb->func();
	cout << pa.use_count() << endl;
	cout << pb.use_count() << endl;
	return 0;
}
#include <thread>
class A {
public:
	A() { cout << "A()" << endl; }
	~A() { cout << "~A()" << endl; }
	void testA() { cout << "好方法!" << endl; }
};
void handler(weak_ptr<A> pw) {
	std::this_thread::sleep_for(std::chrono::seconds(2));
	shared_ptr<A> ps = pw.lock();
	if (ps != nullptr) {
		ps->testA();
	}
	else {
		cout << "对象已析构,非法访问!" << endl;
	}
}
int main() {
	{
		shared_ptr<A> p (new A());
		thread t1(handler, weak_ptr<A> (p));
		std::this_thread::sleep_for(std::chrono::seconds(4));
		t1.detach();
	}
	return 0;
}
#include <memory>
#include <thread>
#include <functional>
template<typename T>
class MyDeletor {
public:
	void operator()(T* ptr) { 
		cout << "call MyDeletor::void operator()(T* ptr)" << endl;
		delete[]ptr;
	}
};
template<typename T>
class MyFileDeletor {
public:
	void operator()(T* ptr) {
		cout << "call MyFileDeletor::void operator()(T* ptr) " << endl;
		fclose(ptr);
	}
};
int main() {
	unique_ptr<int, MyDeletor<int>> ptr1(new int[100]);
	unique_ptr<FILE, MyFileDeletor<FILE>>ptr2(fopen("hello.txt", "w"));
	unique_ptr<int, function<void(int*)>> ptr3(new int[100],
		[](int* ptr)->void {
			cout << "lambda function<void(int*)" << endl;
			delete[]ptr;
		}
	);
	unique_ptr<FILE, function<void(FILE*)>>ptr4(fopen("hello.txt", "w"), 
		[](FILE* p)->void {
			fclose(p);
			cout << "call function<void(FILE*)" << endl;
		}
			);
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yyycqupt

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值