0
点赞
收藏
分享

微信扫一扫

智能指针主动释放多线程问题

开源GIS定制化开发方案 2022-02-26 阅读 16
c++
class A {
public:
    A() {
        cout << "A" << endl;
    }
    ~A() {
        cout << "~A1" << endl;
        sleep(5);
        cout << "~A2" << endl;
    }
};

int main()
{
    std::unique_ptr<A> m_A = make_unique<A>();
    cout << "~A1" << endl;
    m_A.reset(nullptr);
    cout << "~A2" << endl;
    return 0;
}
/** @brief Replace the stored pointer.
       *
       * @param __p  The new pointer to store.
       *
       * The deleter will be invoked if a pointer is already owned.
       */
      void
      reset(pointer __p = pointer()) noexcept
      {
	static_assert(__is_invocable<deleter_type&, pointer>::value,
		      "unique_ptr's deleter must be invocable with a pointer");
	using std::swap;
	swap(_M_t._M_ptr(), __p);
	if (__p != pointer())
	  get_deleter()(std::move(__p));
      }
举报

相关推荐

0 条评论