智能指针之共享指针

非线程安全的共享指针简单实现

#include <iostream>
using namespace std;

template<class T>
class MySharedPtr {
private:
    int* count;
    T* ptr;
public:
    MySharedPtr(T* p = nullptr);//构造函数

    MySharedPtr(const MySharedPtr<T>& other);//拷贝构造函数

    MySharedPtr<T> &operator=(T* p);//赋值运算符

    MySharedPtr<T> &operator=(const MySharedPtr<T>& other);//赋值运算符

    T* operator->();//解引用所存储的指针

    T& operator*();//解引用所存储的指针

    int getReferance() {
        return *count;
    }

    ~MySharedPtr();
};

template<class T>
MySharedPtr<T>::MySharedPtr(T* p) {
    cout << "constructor\n";
    count = new int(1);
    ptr = p;
}

template<class T>
MySharedPtr<T>::MySharedPtr(const MySharedPtr<T>& other) {
    cout << "copy constructor\n";
    if(this != &other) {
        (*other.count)++;
        count = other.count;
        ptr = other.ptr;
    }
}

template<class T>
MySharedPtr<T> &MySharedPtr<T>::operator=(T* p) {
    cout << "operator=1\n";
    if (ptr == p) {
        return *this;
    }
    --*count;
    if (*count == 0) {
        delete count;
        count = nullptr;
        delete ptr;
        ptr = nullptr;
    }
    count = new int(1);
    ptr = p;
    return *this;
}

template<class T>
MySharedPtr<T> &MySharedPtr<T>::operator=(const MySharedPtr<T>& other) {
    cout << "operator=2\n";
    if(this == &other) {
        return *this;
    }
    --*count;
    if (*count == 0) {
        delete count;
        count = nullptr;
        delete ptr;
        ptr = nullptr;
    }
    (*other.count)++;
    ptr = other.ptr;
    count = other.count;
    return *this;
}

template<class T>
T* MySharedPtr<T>::operator->() {
    cout << "operator ->\n";
    return ptr;
}

template<class T>
T& MySharedPtr<T>::operator*() {
    cout << "operator *\n";
    return *ptr;
}

template<class T>
MySharedPtr<T>::~MySharedPtr() {
    cout << "desconstructor ptr:" << ptr << " count:" << *count << "\n";
    if (--*count == 0) {
        delete count;
        count = nullptr;
        delete ptr;
        ptr = nullptr;
    }
}

class Test {
private:
public:
    Test() {}
    ~Test() {}
    void print() {
        cout << "print\n";
    }
};

int main() {
    MySharedPtr<Test> ptr1(new Test());
    cout << "ptr1 count:" << ptr1.getReferance() << "\n";
    MySharedPtr<Test> ptr2 = ptr1;//拷贝构造函数
    cout << "ptr1 count:" << ptr1.getReferance() << "\n";
    MySharedPtr<Test> ptr3;
    ptr3 = ptr2;
    cout << "ptr1 count:" << ptr1.getReferance() << "\n";

    MySharedPtr<Test> ptr4(new Test());
    cout << "ptr4 count:" << ptr4.getReferance() << "\n";
    MySharedPtr<Test> ptr5 = ptr1;
    cout << "ptr1 count:" << ptr1.getReferance() << "\n";
    MySharedPtr<Test> ptr6 = new Test();
    cout << "ptr6 count:" << ptr6.getReferance() << "\n";
    ptr5 = new Test();
    cout << "ptr1 count:" << ptr1.getReferance() << "\n";
    cout << "ptr5 count:" << ptr5.getReferance() << "\n";

    ptr1->print();
    (*ptr1).print();
}

线程安全实现源码

摘自MacOSX13.0.sdk

template <class _Tp>
inline _LIBCPP_INLINE_VISIBILITY _Tp
__libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT
{
#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
    return __atomic_add_fetch(&__t, 1, __ATOMIC_RELAXED);
#else
    return __t += 1;
#endif
}

template <class _Tp>
inline _LIBCPP_INLINE_VISIBILITY _Tp
__libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT
{
#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
    return __atomic_add_fetch(&__t, -1, __ATOMIC_ACQ_REL);
#else
    return __t -= 1;
#endif
}

shared_ptr在引用计数器自增采用宽松操作:memory_order_relaxed
带标签 memory_order_relaxed 的原子操作无同步操作;它们不会在共时的内存访问间强加顺序。它们只保证原子性和修改顺序一致性。

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值