美文网首页
weak_ptr lock 理解

weak_ptr lock 理解

作者: 爱玩保龄球 | 来源:发表于2020-02-20 21:25 被阅读0次

创建新的 std::shared_ptr 对象,它共享被管理对象的所有权。若无被管理对象,即 *this 为空,则返回亦为空的 shared_ptr

等效地返回 expired() ? shared_ptr<T>() : shared_ptr<T>(*this) ,原子地执行。

参数

(无)

返回值

std::weak_ptr::expired 返回 false 则为共享被占有对象所有权的 shared_ptr 。否则返回默认构造的 T 类型的 shared_ptr

注意

此函数和 std::shared_ptr 的构造函数可能获得 std::weak_ptr 所指向的被管理对象的临时所有权。区别是 std::shared_ptr 的构造函数在其 std::weak_ptr 为空时抛异常,而 std::weak_ptr<T>::lock() 构造空的 std::shared_ptr<T> 。

示例代码

#include <iostream>
#include <memory>
 
void observe(std::weak_ptr<int> weak)
{
    if (auto observe = weak.lock()) {
        std::cout << "\tobserve() able to lock weak_ptr<>, value=" << *observe << "\n";
    } else {
        std::cout << "\tobserve() unable to lock weak_ptr<>\n";
    }
}
 
int main()
{
    std::weak_ptr<int> weak;
    std::cout << "weak_ptr<> not yet initialized\n";
    observe(weak);
 
    {
        auto shared = std::make_shared<int>(42);
        weak = shared;
        std::cout << "weak_ptr<> initialized with shared_ptr.\n";
        observe(weak);
    }
 
    std::cout << "shared_ptr<> has been destructed due to scope exit.\n";
    observe(weak);
}

参考链接

https://zh.cppreference.com/w/cpp/memory/weak_ptr/lock

输出

weak_ptr<> not yet initialized
    observe() unable to lock weak_ptr<>
weak_ptr<> initialized with shared_ptr.
    observe() able to lock weak_ptr<>, value=42
shared_ptr<> has been destructed due to scope exit.
    observe() unable to lock weak_ptr<>
Program ended with exit code: 0

扩展思考,变成class 之后

#include <iostream>
#include <memory>
#include <string>
#include <unordered_map>
#include <utility>

class testTask{
public:
    int taskId;
};

using testTaskPtr = std::shared_ptr<testTask>;

void observe(std::weak_ptr<int> weak)
{
    if (auto observe = weak.lock()) {
        std::cout << "\tobserve() able to lock weak_ptr<>, value=" << *observe << "\n";
    } else {
        std::cout << "\tobserve() unable to lock weak_ptr<>\n";
    }
}

void observe_c(std::weak_ptr<testTask> weak)
{
    if (auto observe = weak.lock()) {
        std::cout << "\tobserve_c() able to lock weak_ptr<>, addr=" << observe << "\n";
    } else {
        std::cout << "\tobserve_c() unable to lock weak_ptr<>\n";
    }
}

int main()
{
    std::weak_ptr<int> weak;
    std::cout << "weak_ptr<> not yet initialized\n";
    observe(weak);
 
    {
        auto shared = std::make_shared<int>(42);
        weak = shared;
        std::cout << "weak_ptr<> initialized with shared_ptr.\n";
        observe(weak);
    }
 
    std::cout << "shared_ptr<> has been destructed due to scope exit.\n";
    observe(weak);
    
    
   
    std::unordered_map<std::string,testTaskPtr> test_map;
    
    auto test_share = std::make_shared<testTask>();
    std::cout << "weak_ptr<> class initialized with shared_ptr.\n";
    observe_c(test_share);
    test_map["test"] = test_share;
    test_map.erase("test");
    std::cout << "weak_ptr<> class erase with shared_ptr.\n";
    observe_c(test_share);
    
    test_share = nullptr;
    std::cout << "test_share = nullptr \n";
    observe_c(test_share);
   // test_map.insert (std::make_pair<std::string,testTask>("test_share",test_share));
    
}

输出

weak_ptr<> not yet initialized
    observe() unable to lock weak_ptr<>
weak_ptr<> initialized with shared_ptr.
    observe() able to lock weak_ptr<>, value=42
shared_ptr<> has been destructed due to scope exit.
    observe() unable to lock weak_ptr<>
weak_ptr<> class initialized with shared_ptr.
    observe_c() able to lock weak_ptr<>, addr=0x10062b078
weak_ptr<> class erase with shared_ptr.
    observe_c() able to lock weak_ptr<>, addr=0x10062b078
test_share = nullptr 
    observe_c() unable to lock weak_ptr<>
Program ended with exit code: 0

参考代码

相关文章

  • weak_ptr lock 理解

    创建新的 std::shared_ptr 对象,它共享被管理对象的所有权。若无被管理对象,即 *this 为空,则...

  • weak_ptr的使用场景

    weak_ptr只能从shared_ptr对象构建。 weak_ptr并不影响动态对象的生命周期,即其存在与否并不...

  • 理解synchronized 和lock

    锁是并发编程中经常用到的,本文主要分析下synchronized和lock锁机制的区别。 性能区别 分两种场景来比...

  • MySQL:理解MDL Lock

    本文基于源码版本5.7.14水平有限,有误请谅解笔者已经将加好MDL 获取过程和释放过程的版本放到了github如...

  • day24

    1:多线程(理解) (1)JDK5以后的针对线程的锁定操作和释放操作 Lock锁:void lock(): 获取锁...

  • Java多线程之Lock深入理解

    1 Lock原理深入理解 Java中已经有了synchronized重量级锁,那么为什么还得有Lock,之所以引入...

  • synchronized和lock简单理解

    synchronized(同步锁) 思考:锁什么?锁对象 可能锁对象包括:this,临界资源对象(所有线程可能访问...

  • python之理解GIL

    python之理解GIL 1、GIL简介 GIL的全称为Global Interpreter Lock,全局解释器...

  • Java 锁

    Lock Lock.lock 进入锁 Lock.unLock 释放锁 Lock.tryLock 获取锁 (ret...

  • 1 - 智能指针

    https://www.nowcoder.com/discuss/418992 智能指针的实现,weak_ptr为...

网友评论

      本文标题:weak_ptr lock 理解

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