【C++編程】boost::intrusive_ptr

boost::intrusive_ptr

shared_ptr最大的陷阱就是用同一個裸指針創建了多個shared_ptr,這會導致這些shared_ptr析構時,因爲每個shared_ptr都有自己引用計數,導致這個裸指針多次銷燬所以不能用一個裸指針來創建多個shared_ptr,但卻可以來創建多個intrusive_ptr,因爲所使用的對象通過繼承包含引用計數功能的基類(將引用計數作爲對象的內部成員變量,大家共用一個計數器,就不會出現每個對象裏都有自己的計數問題。intrusive_ptr是“侵入式”的,所以它所指向的對象,必須繼承包含引用計數功能的基類;而shared_ptr不是“侵入式”的,可指向任何類型的對象。

“侵入式”的引用計數智能指針,實際不提供引用計數功能,要求被存儲的對象類自己實現引用計數功能,對象類需要提供intrusive_ptr_add_refintrusive_ptr_release友元函數接口供boost::intrusive_ptr調用來間接管理引用計數。所以,首先需要實現一個基類來定義上述兩個函數來提供引用計數功能,然後再派生出子類進行業務處理,intrusive_ptr的構造函數和reset()裏有一個add_ref參數,表示是否增加引用計數,如果add_ref=false, 那麼intrusive_ptr就是weak_ptr。boost庫裏提供了一個輔助類intrusive_ref_counter,用來實現引用計數的工作,在頭文件<boost/smart_ptr/intrusive_ref_counter>裏

 

實例:

#include <assert.h>
#include <atomic>
#include <iostream>
#include <string>
#include <boost/intrusive_ptr.hpp>

class ReferenceCounter {
public:
	friend void intrusive_ptr_add_ref(ReferenceCounter *p) 
	{
		std::cout << "Call intrusive_ptr_add_ref" << std::endl;
		assert(p);
		assert(p->ref_count >= 0);
		++p->ref_count;
	}

	friend void intrusive_ptr_release(ReferenceCounter *p) 
	{
		std::cout << "Call intrusive_ptr_release" << std::endl;
		assert(p);
		assert(p->ref_count > 0);
		if (--p->ref_count == 0)
		{
			delete p;
		}
	}

	ReferenceCounter() : ref_count(0)
	{
		std::cout << "Reference Counter Constructor" << std::endl;
	}

	ReferenceCounter(const ReferenceCounter &other)
	{
		std::cout << "Reference Counter Copy Constructor" << std::endl;
	}

	ReferenceCounter &operator=(const ReferenceCounter &other)
	{
		std::cout << "Reference Counter Assignment Operator" << std::endl;
	}

	~ReferenceCounter()
	{
		std::cout << "Reference Counter Destructor" << std::endl;
	};

	int RefCount()
	{
		return ref_count;
	}

private:
	std::atomic_int ref_count;
};

class ProcessData : public ReferenceCounter {
public:
	ProcessData(int id, std::string info) : m_id(id), m_info(info)
	{
		std::cout << "Process Data Constructor" << std::endl;
	}

	ProcessData(const ProcessData &other)
	{
		std::cout << "Process Data Copy Constructor" << std::endl;
		m_id = other.m_id;
		m_info = other.m_info;
	}

	const ProcessData operator=(const ProcessData &other)
	{
		std::cout << "Process Data Assignment Operator" << std::endl;
		m_id = other.m_id;
		m_info = other.m_info;
	}

	~ProcessData()
	{
		std::cout << "Process Data Destructor" << std::endl;
	}

private:
	int m_id;
	std::string m_info;
};

int main()
{
	boost::intrusive_ptr<ProcessData> ptr(new ProcessData(1, "a"));
	std::cout << "******************" << std::endl;
	std::cout << "ref_count = " << ptr->RefCount() << std::endl;
	std::cout << "******************" << std::endl;
	{
		boost::intrusive_ptr<ProcessData> ptrCopy(ptr.get());
		std::cout << "ref_count after copy constructed = " << ptrCopy->RefCount() << std::endl;
	}

	std::cout << "******************" << std::endl;
	std::cout << "ref_count = " << ptr->RefCount() << std::endl;
	std::cout << "******************" << std::endl;

	{
		boost::intrusive_ptr<ProcessData> ptrAssignment = ptr;
		std::cout << "ref_count after assignment = " << ptrAssignment->RefCount() << std::endl;
	}

	std::cout << "******************" << std::endl;
	std::cout << "ref_count = " << ptr->RefCount() << std::endl;
	std::cout << "******************" << std::endl;

	{
		boost::intrusive_ptr<ProcessData> ptrWeak(ptr.get(), false);
		std::cout << "ref_count after construct weak_ptr = " << ptrWeak->RefCount() << std::endl;
	}

	std::cout << "******************" << std::endl;
	std::cout << "ref_count = " << ptr->RefCount() << std::endl;
	std::cout << "******************" << std::endl;

	return 0;
}

編譯:

[root@node01 demo]# g++  -std=c++11 -lboost_system -lpthread -I/usr/local/include/boost -L/usr/local/lib  demo.cc
[root@node01 demo]# ./a.out 
Reference Counter Constructor
Process Data Constructor
Call intrusive_ptr_add_ref
******************
ref_count = 1
******************
Call intrusive_ptr_add_ref
ref_count after copy constructed = 2
Call intrusive_ptr_release
******************
ref_count = 1
******************
Call intrusive_ptr_add_ref
ref_count after assignment = 2
Call intrusive_ptr_release
******************
ref_count = 1
******************
ref_count after construct weak_ptr = 1
Call intrusive_ptr_release
Reference Counter Destructor
******************
ref_count = 0
******************
Call intrusive_ptr_release
a.out: demo.cc:21: void intrusive_ptr_release(ReferenceCounter*): Assertion `p->ref_count > 0' failed.
Aborted

參考資料

1. boost::intrusive_ptr的用法

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章