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

class Counter {
public:
  friend void intrusive_ptr_add_ref(Counter *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(Counter *p)
  {
    std::cout << "Call intrusive_ptr_release" << std::endl;
    assert(p);
    assert(p->ref_count > 0);
    if (--p->ref_count == 0)
    {
      delete p;
    }
  }

  Counter() : ref_count(0) { cout << "Counter Constructor" << endl; }
  Counter(const Counter &other) { cout << "Counter Copy Constructor" << endl; }
  Counter &operator=(const Counter &other) { cout << "Counter Assignment Operator" << endl;}
  ~Counter() { cout << "Reference Counter Destructor" << endl; };
  int RefCount() { return ref_count; }
private:
  atomic_int ref_count;
};

class ProcessData : public Counter {
public:
  ProcessData(int id, string info) : m_id(id), m_info(info) { cout << "Process Data Constructor" << endl; }
  ProcessData(const ProcessData &other)
  {
    cout << "Process Data Copy Constructor" << endl;
    m_id = other.m_id;
    m_info = other.m_info;
  }

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

  ~ProcessData() { cout << "Process Data Destructor" << endl; }
private:
  int m_id;
  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的用法

posted @ 2023-11-28 17:55  苏格拉底的落泪  阅读(27)  评论(0编辑  收藏  举报