面试常见——手写智能指针

template <class T>
class SharedPtr
{
public:
    SharedPtr(T* ptr = nullptr)
        : _ptr(ptr)
        , _pRefCount(new int(1))
        , _pMutex(new mutex)
    {}

    ~SharedPtr()
    {
        Release();
    }

    SharedPtr(const SharedPtr<T>& sp)
        : _ptr(sp._ptr)
        , _pRefCount(sp._pRefCount)
        , _pMutex(sp._pMutex)
    {
        AddRefCount();
    }

    // 赋值
    SharedPtr<T>& operator=(const SharedPtr<T>& sp)
    {
        if (_ptr != sp._ptr)
        {
            // 释放管理的旧资源
            Release();
            // 共享管理新对象的资源,并增加引用计数
            _ptr = sp._ptr;
            _pRefCount = sp._pRefCount;
            _pMutex = sp._pMutex;
            AddRefCount();
        }
        return *this;
    }


    T& operator*()
    {
        return *_ptr;
    }

    T* operator->()
    {
        return _ptr;
    }

    int UseCount()
    {
        return *_pRefCount;
    }

    T* Get()
    {
        return _ptr;
    }

    void AddRefCount()
    {
        // 加锁或者使用加1的原子操作
        _pMutex->lock();
        ++(*_pRefCount);
        _pMutex->unlock();
    }

private:
    void Release()
    {
        bool deleteflag = false;
        // 引用计数减1,如果减到0,则释放资源
        _pMutex->lock();
        if (--(*_pRefCount) == 0)
        {
            delete _ptr;
            delete _pRefCount;
            deleteflag = true;
        }
        _pMutex->unlock();

        if (deleteflag == true)
            delete _pMutex;
    }
private:
    int* _pRefCount; // 引用计数
    T* _ptr; // 指向管理资源的指针
    mutex* _pMutex; // 互斥锁
};

参考:https://blog.csdn.net/qq_56663697/article/details/123964427

posted @ 2022-05-27 10:49  suppersam  阅读(63)  评论(0编辑  收藏  举报