C++11 std::shared_ptr总结与使用 智能指针

最近看代码,智能指针用的比较多,自己平时用的少,周末自己总结总结。方便后续使用。

std::shared_ptr大概总结有以下几点:

(1) 智能指针主要的用途就是方便资源的管理,自动释放没有指针引用的资源。

(2) 使用引用计数来标识是否有多余指针指向该资源。(注意,shart_ptr本身指针会占1个引用)

(3) 在赋值操作中, 原来资源的引用计数会减一,新指向的资源引用计数会加一。

std::shared_ptr<Test> p1(new Test);

std::shared_ptr<Test> p2(new Test);

p1 = p2;

(4) 引用计数加一/减一操作是原子性的,所以线程安全的。

(5) make_shared要优于使用new,make_shared可以一次将需要内存分配好。

std::shared_ptr p = std::make_shared(); std::shared_ptr p(new Test); (6) std::shared_ptr的大小是原始指针的两倍,因为它的内部有一个原始指针指向资源,同时有个指针指向引用计数。

(7) 引用计数是分配在动态分配的,std::shared_ptr支持拷贝,新的指针获可以获取前引用计数个数。

下面是一段示例代码,注释详细:

#include <iostream>
#include <memory>
#include <thread>
#include <chrono>
#include <mutex>

struct Test
{
    Test() { std::cout << "  Test::Test()\n"; }
    ~Test() { std::cout << "  Test::~Test()\n"; }
};

//线程函数
void thr(std::shared_ptr<Test> p)
{
    //线程暂停1s
    std::this_thread::sleep_for(std::chrono::seconds(1));

    //赋值操作, shared_ptr引用计数use_cont加1(c++11中是原子操作)
    std::shared_ptr<Test> lp = p;
    {
        //static变量(单例模式),多线程同步用
        static std::mutex io_mutex;

        //std::lock_guard加锁
        std::lock_guard<std::mutex> lk(io_mutex);
        std::cout << "local pointer in a thread:\n"
                  << "  lp.get() = " << lp.get()
                  << ", lp.use_count() = " << lp.use_count() << '\n';
    }
}

int main()
{
    //使用make_shared一次分配好需要内存
    std::shared_ptr<Test> p = std::make_shared<Test>();
    //std::shared_ptr<Test> p(new Test);

    std::cout << "Created a shared Test\n"
              << "  p.get() = " << p.get()
              << ", p.use_count() = " << p.use_count() << '\n';

    //创建三个线程,t1,t2,t3
    //形参作为拷贝, 引用计数也会加1
    std::thread t1(thr, p), t2(thr, p), t3(thr, p);
    std::cout << "Shared ownership between 3 threads and released\n"
              << "ownership from main:\n"
              << "  p.get() = " << p.get()
              << ", p.use_count() = " << p.use_count() << '\n';
    //等待结束
    t1.join(); t2.join(); t3.join();
    std::cout << "All threads completed, the last one deleted\n";

    return 0;
}

答案:

  Test::Test()
Created a shared Test
  p.get() = 0x1d1500, p.use_count() = 1
Shared ownership between 3 threads and released
ownership from main:
  p.get() = 0x1d1500, p.use_count() = 4
local pointer in a thread:
  lp.get() = 0x1d1500, lp.use_count() = 7
local pointer in a thread:
  lp.get() = 0x1d1500, lp.use_count() = 5
local pointer in a thread:
  lp.get() = 0x1d1500, lp.use_count() = 3
All threads completed, the last one deleted
  Test::~Test()

样例2

#include <iostream>
#include <memory>
#include <type_traits>

struct C
{
  C(int i) : i(i) {} // < 需要构造函数 (C++20 前)
  int i;
};

int main()
{
    auto sp = std::make_shared<C>(12);//12 是传给C的构造函数了

    static_assert(std::is_same_v<decltype(sp), std::shared_ptr<C>>);

    std::cout << sp->i << '\n';
}
文章目录