• std::lock_guard shared_ptr


    #include <iostream>
    #include <memory>
    #include <thread>
    #include <chrono>
    #include <mutex>
    
    struct Test
    {
        Test() { std::cout << "  Test::Test()
    "; }
        ~Test() { std::cout << "  Test::~Test()
    "; }
    };
    
    //线程函数
    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:
    "
                      << "  lp.get() = " << lp.get()
                      << ", lp.use_count() = " << lp.use_count() << '
    ';
        }
    }
    
    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
    "
                  << "  p.get() = " << p.get()
                  << ", p.use_count() = " << p.use_count() << '
    ';
    
        //创建三个线程,t1,t2,t3
        //形参作为拷贝, 引用计数也会加1
        std::thread t1(thr, p), t2(thr, p), t3(thr, p);
        std::cout << "Shared ownership between 3 threads and released
    "
                  << "ownership from main:
    "
                  << "  p.get() = " << p.get()
                  << ", p.use_count() = " << p.use_count() << '
    ';
        //等待结束
        t1.join(); t2.join(); t3.join();
        std::cout << "All threads completed, the last one deleted
    ";
    
        return 0;
    }
    root@ubuntu:~/c++# ./share4
      Test::Test()
    Created a shared Test
      p.get() = 0xaaaaccc75e80, p.use_count() = 1
    Shared ownership between 3 threads and released
    ownership from main:
      p.get() = 0xaaaaccc75e80, p.use_count() = 4
    local pointer in a thread:
      lp.get() = 0xaaaaccc75e80, lp.use_count() = 5
    local pointer in a thread:
      lp.get() = 0xaaaaccc75e80, lp.use_count() = 4
    local pointer in a thread:
      lp.get() = 0xaaaaccc75e80, lp.use_count() = 3
    All threads completed, the last one deleted
      Test::~Test()
    #include <iostream>
    #include <memory>
    #include <thread>
    #include <chrono>
    #include <mutex>
    
    struct Test
    {
        Test() { std::cout << "  Test::Test()
    "; }
        ~Test() { std::cout << "  Test::~Test()
    "; }
    };
    
    //线程函数
    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:
    "
                      << "  lp.get() = " << lp.get()
                      << ", lp.use_count() = " << lp.use_count() << '
    ';
        }
    }
    
    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
    "
                  << "  p.get() = " << p.get()
                  << ", p.use_count() = " << p.use_count() << '
    ';
    
        //形参作为拷贝, 引用计数也会加1
        std::thread t1(thr, p);
        std::cout << "Shared ownership between 3 threads and released
    "
                  << "ownership from main:
    "
                  << "  p.get() = " << p.get()
                  << ", p.use_count() = " << p.use_count() << '
    ';
        std::cout << "All threads completed, the last one deleted
    ";
         t1.join();
        return 0;
    }
     Test::Test()
    Created a shared Test
      p.get() = 0xaaaadf9e3e80, p.use_count() = 1
    Shared ownership between 3 threads and released
    ownership from main:
      p.get() = 0xaaaadf9e3e80, p.use_count() = 2
    All threads completed, the last one deleted
    local pointer in a thread:
      lp.get() = 0xaaaadf9e3e80, lp.use_count() = 3
      Test::~Test()
  • 相关阅读:
    js 实现 间隙滚动效果
    js实现无缝滚动
    js 实现弹出层效果
    jQuery实现网页定位导航
    右侧定位的样式
    jQuery实现点击按钮展开和收起
    js实现点击按钮控制展开与收起.
    【BZOJ1202】[HNOI2005]狡猾的商人 带权并查集
    【BZOJ1067】[SCOI2007]降雨量 RMQ+特判
    【BZOJ1367】[Baltic2004]sequence 左偏树
  • 原文地址:https://www.cnblogs.com/dream397/p/14685321.html
Copyright © 2020-2023  润新知