{"id":192,"date":"2022-01-27T23:40:23","date_gmt":"2022-01-27T15:40:23","guid":{"rendered":"http:\/\/139.224.63.49\/?p=192"},"modified":"2022-01-27T23:40:23","modified_gmt":"2022-01-27T15:40:23","slug":"%e8%87%aa%e5%b7%b1%e6%89%8b%e5%8a%a8%e5%ae%9e%e7%8e%b0%e4%b8%80%e4%b8%aa%e7%ae%80%e5%8d%95%e7%89%88%e6%9c%ac%e7%9a%84shared_ptr","status":"publish","type":"post","link":"http:\/\/iamnear.top\/?p=192","title":{"rendered":"\u81ea\u5df1\u624b\u52a8\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7248\u672c\u7684shared_ptr"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>template&lt;typename T>\nclass my_shared_ptr{\npublic:\n    explicit my_shared_ptr(T* p=nullptr){\n        m_p = p;\n        m_cnt = new size_t(1);\n        m_mutex = new mutex();\n    }\n    my_shared_ptr(const my_shared_ptr&amp; other){\n        m_p = other.m_p;\n        m_cnt = other.m_cnt;\n        m_mutex = other.m_mutex;\n        acquire();\n    }\n    my_shared_ptr&amp; operator=(const my_shared_ptr&amp; other){\n        if(m_p==other.m_p)\n            return *this;\n        release();\n        m_p = other.m_p;\n        m_cnt = other.m_cnt;\n        m_mutex = other.m_mutex;\n        acquire();\n        return *this;\n    }\n    my_shared_ptr(my_shared_ptr&amp;&amp; o){\n        m_p = o.m_p;\n        m_cnt = o.m_cnt;\n        m_mutex = o.m_mutex;\n        o.m_p = nullptr;\n        o.m_cnt = new size_t(1);\n        o.m_mutex = new mutex();\n    }\n    my_shared_ptr&amp; operator=(my_shared_ptr&amp;&amp; o){\n        if(m_p==o.m_p)\n            return *this;\n        release();\n        m_p = o.m_p;\n        m_cnt = o.m_cnt;\n        m_mutex = o.m_mutex;\n        o.m_p = nullptr;\n        o.m_cnt = new size_t(1);\n        o.m_mutex = new mutex();\n        return *this;\n    }\n    size_t use_count() const{\n        return *m_cnt;\n    }\n    ~my_shared_ptr(){\n        release();\n    }\n    T* get() const{\n        return m_p;\n    }\n    T&amp; operator*() const{\n        return *m_p;\n    }\n    T* operator->() const{\n        return m_p;\n    }\nprivate:\n    void acquire(){\n        m_mutex->lock();\n        ++(*m_cnt);\n        m_mutex->unlock();\n    }\n    void release(){\n        bool deleted = false;\n        m_mutex->lock();\n        --(*m_cnt);\n        if(*m_cnt==0){\n            deleted = true;\n            delete m_cnt;\n            if(m_p!=nullptr)\n                delete m_p;\n        }\n        m_mutex->unlock();\n        if(deleted){\n            delete m_mutex;\n        }\n    }\n    size_t *m_cnt;\n    T *m_p;\n    mutex* m_mutex;\n};<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":{"0":"post-192","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"hentry","6":"category-uncategorized","7":"h-entry","9":"h-as-article"},"_links":{"self":[{"href":"http:\/\/iamnear.top\/index.php?rest_route=\/wp\/v2\/posts\/192","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/iamnear.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/iamnear.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/iamnear.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/iamnear.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=192"}],"version-history":[{"count":1,"href":"http:\/\/iamnear.top\/index.php?rest_route=\/wp\/v2\/posts\/192\/revisions"}],"predecessor-version":[{"id":194,"href":"http:\/\/iamnear.top\/index.php?rest_route=\/wp\/v2\/posts\/192\/revisions\/194"}],"wp:attachment":[{"href":"http:\/\/iamnear.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=192"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/iamnear.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=192"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/iamnear.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=192"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}