美文网首页
智能指针-shared_ptr

智能指针-shared_ptr

作者: cp3_1dbc | 来源:发表于2018-10-23 17:01 被阅读0次

    代码示例

    #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()
    

    相关文章

      网友评论

          本文标题:智能指针-shared_ptr

          本文链接:https://www.haomeiwen.com/subject/sslybftx.html