智能指针-shared_ptr

代码示例

#include <iostream>
#include <memory>
#include <thread>
#include <chrono>
#include <mutex>
 
struct Base
{
    Base() { std::cout << "  Base::Base()\n"; }
    // 注意:此处非虚析构函数 OK
    ~Base() { std::cout << "  Base::~Base()\n"; }
};
 
struct Derived: public Base
{
    Derived() { std::cout << "  Derived::Derived()\n"; }
    ~Derived() { std::cout << "  Derived::~Derived()\n"; }
};
 
void thr(std::shared_ptr<Base> p)
{
    std::this_thread::sleep_for(std::chrono::seconds(1));
    std::shared_ptr<Base> lp = p; // 线程安全,虽然自增共享的 use_count
    {
        static std::mutex io_mutex;
        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';
    }
}

void copy(std::shared_ptr<Base> p)
{
  std::cout << "copy function\n"
            << ",  p.get() = " << p.get()
            << ",  p.use_count() = " << p.use_count() << "\n\n\n";
}
 
int main()
{
    std::shared_ptr<Base> p0 = std::shared_ptr<Base>(new Derived);
    std::cout << "Created a shared Derived (as a pointer to Base)\n"
              << "  p0.get() = " << p0.get()
              << ", p0.use_count() = " << p0.use_count() << "\n\n\n";


    // "operator =" will increase ref count 
    std::shared_ptr<Base> p01 = p0;
    std::cout << "test operator =\n"
              << "p0.get() = " << p0.get()
              << ",  p0.use_count() = " << p0.use_count() 
              << ",  p01.get() = " << p01.get()
              << ",  p01.use_count() = " << p01.use_count() << "\n\n\n";


    // "copy construct" will increase ref count 
    std::shared_ptr<Base> p02(p0);
    std::cout << "test copy construct\n"
              << "p0.get() = " << p0.get()
              << ",  p0.use_count() = " << p0.use_count() 
              << ",  p02.get() = " << p02.get()
              << ",  p02.use_count() = " << p02.use_count() << "\n\n\n";

    copy(p0);
    std::cout << "test copy construct 1\n"
              << ",  p0.get() = " << p0.get()
              << ",  p0.use_count() = " << p0.use_count() << "\n\n\n";

    // reset will decrease ref count
    p02.reset();
    std::cout << "test reset\n"
              << "p0.get() = " << p0.get()
              << ",  p0.use_count() = " << p0.use_count() 
              << ",  p02.get() = " << p02.get()
              << ",  p02.use_count() = " << p02.use_count() << "\n\n\n";


    // destruct will decrease ref count
    {
      std::shared_ptr<Base> p03(p0);
      std::cout << "Created a shared Derived (as a pointer to Base)\n"
                << "p0.get() = " << p0.get()
                << ",  p0.use_count() = " << p0.use_count() 
                << ",  p03.get() = " << p03.get()
                << ",  p03.use_count() = " << p03.use_count() << "\n";
    }
    std::cout << "Created a shared Derived (as a pointer to Base)\n"
              << ",  p0.get() = " << p0.get()
              << ",  p0.use_count() = " << p0.use_count() << "\n\n\n";

    std::shared_ptr<Base> p = std::make_shared<Derived>();
 
    std::cout << "Created a shared Derived (as a pointer to Base)\n"
              << "  p.get() = " << p.get()
              << ", p.use_count() = " << p.use_count() << '\n';
    std::thread t1(thr, p), t2(thr, p), t3(thr, p);
    p.reset(); // 从 main 释放所有权
    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 Derived\n";
}

运行结果

  Base::Base()
  Derived::Derived()
Created a shared Derived (as a pointer to Base)
  p0.get() = 0x25a8c20, p0.use_count() = 1


test operator =
p0.get() = 0x25a8c20,  p0.use_count() = 2,  p01.get() = 0x25a8c20, p01.use_count() = 2


test copy construct
p0.get() = 0x25a8c20,  p0.use_count() = 3,  p02.get() = 0x25a8c20, p02.use_count() = 3


copy function
,  p.get() = 0x25a8c20,  p.use_count() = 4


Created a shared Derived (as a pointer to Base)
,  p0.get() = 0x25a8c20, p0.use_count() = 3


test reset
p0.get() = 0x25a8c20,  p0.use_count() = 2,  p02.get() = 0, p02.use_count() = 0


Created a shared Derived (as a pointer to Base)
p0.get() = 0x25a8c20,  p0.use_count() = 3,  p03.get() = 0x25a8c20,  p03.use_count() = 3
Created a shared Derived (as a pointer to Base)
,  p0.get() = 0x25a8c20,  p0.use_count() = 2


  Base::Base()
  Derived::Derived()
Created a shared Derived (as a pointer to Base)
  p.get() = 0x25a8c70, p.use_count() = 1
Shared ownership between 3 threads and released
ownership from main:
  p.get() = 0, p.use_count() = 0
local pointer in a thread:
  lp.get() = 0x25a8c70, lp.use_count() = 4
local pointer in a thread:
  lp.get() = 0x25a8c70, lp.use_count() = 3
local pointer in a thread:
  lp.get() = 0x25a8c70, lp.use_count() = 2
  Derived::~Derived()
  Base::~Base()
All threads completed, the last one deleted Derived
  Derived::~Derived()
  Base::~Base()
chris@chris-ubuntu:/media/sf_shared/myspace/myspace/test/c++11$ 
chris@chris-ubuntu:/media/sf_shared/myspace/myspace/test/c++11$ 
chris@chris-ubuntu:/media/sf_shared/myspace/myspace/test/c++11$ g++ -o shared_ptr shared_ptr.cc -std=c++11 -lpthread
chris@chris-ubuntu:/media/sf_shared/myspace/myspace/test/c++11$ ./shared_ptr
  Base::Base()
  Derived::Derived()
Created a shared Derived (as a pointer to Base)
  p0.get() = 0x67bc20, p0.use_count() = 1


test operator =
p0.get() = 0x67bc20,  p0.use_count() = 2,  p01.get() = 0x67bc20, p01.use_count() = 2


test copy construct
p0.get() = 0x67bc20,  p0.use_count() = 3,  p02.get() = 0x67bc20, p02.use_count() = 3


copy function
,  p.get() = 0x67bc20,  p.use_count() = 4


test copy construct 1
,  p0.get() = 0x67bc20, p0.use_count() = 3


test reset
p0.get() = 0x67bc20,  p0.use_count() = 2,  p02.get() = 0, p02.use_count() = 0


Created a shared Derived (as a pointer to Base)
p0.get() = 0x67bc20,  p0.use_count() = 3,  p03.get() = 0x67bc20,  p03.use_count() = 3
Created a shared Derived (as a pointer to Base)
,  p0.get() = 0x67bc20,  p0.use_count() = 2


  Base::Base()
  Derived::Derived()
Created a shared Derived (as a pointer to Base)
  p.get() = 0x67bc70, p.use_count() = 1
Shared ownership between 3 threads and released
ownership from main:
  p.get() = 0, p.use_count() = 0
local pointer in a thread:
  lp.get() = 0x67bc70, lp.use_count() = 4
local pointer in a thread:
  lp.get() = 0x67bc70, lp.use_count() = 3
local pointer in a thread:
  lp.get() = 0x67bc70, lp.use_count() = 2
  Derived::~Derived()
  Base::~Base()
All threads completed, the last one deleted Derived
  Derived::~Derived()
  Base::~Base()
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 0 现摘录一篇不错的讲Shared_ptr的文章,要点记录如下: 智能指针是为了管理针对于异常、线程间等的内存,防...
    大二哈阅读 4,027评论 0赞 1
  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AI阅读 16,484评论 3赞 119
  • In the game, the player plays an experienced sharpshooter...
    Green_Mage阅读 322评论 0赞 0
  • 黎明的夜色中,汽车缓缓开动。 我身心俱疲,却无心睡眠。回味着昨夜与显山的长谈。有一句戏谑,让我心头一颤,思绪飘回到...
    问挚阅读 947评论 0赞 3
  • 不知道怎么,突然迷上了珍珠奶茶,不想什么正宗,不想什么混搭,总在丝滑的奶茶里加上其他乱七八糟的东西,只要自己喜欢!...
    小九娘阅读 567评论 0赞 0

友情链接更多精彩内容