C++小型线程安全双向链表实例

#pragma once

#include <mutex>

namespace Yanhai {
    // 节点结构
    template <typename T>
    struct Node {
        T TypePtr;
        Node* Next;
        Node* Prev;
        Node(T Value) : TypePtr(Value), Next(nullptr), Prev(nullptr) {}
        ~Node() {
            /*
             * 很明显,这里的 TypePtr 必须是指针类型(使用者可以随便定义属于其自己的方法)
             * 注意:这么做,其实是很危险的,容易出现段错误
             */
            if (TypePtr) delete TypePtr;
        }
    };

    // 双向链表类
    template <typename T>
    class ThreadSafeDoublyLinkedList final {
        typedef Node<T> Element;

    public:
        // 迭代器类
        class Iterator final {
        private:
            Element* Current;
            std::mutex* Mutex;

        public:
            // 构造函数
            Iterator() = delete;
            Iterator(Iterator&& It) : Current(It.Current), Mutex(It.Mutex) {}
            Iterator(const Iterator& It) : Current(It.Current), Mutex(It.Mutex) {}
            Iterator(Element* NodePtr, std::mutex* MutexPtr) : Current(NodePtr), Mutex(MutexPtr) {}

            // 重载*操作符
            T& operator*() {
                if (!Current) {
                    throw std::runtime_error("Failed to get current node");
                }
                return Current->TypePtr;
            }

            // 重载++前缀操作符
            Iterator& operator++() {
                std::lock_guard<std::mutex> Lock(*Mutex);
                if (!Current) {
                    throw std::runtime_error("Failed to get current node");
                }
                Current = Current->Next;
                return *this;
            }

            // 重载++后缀操作符
            Iterator operator++(int) {
                std::lock_guard<std::mutex> Lock(*Mutex);
                if (!Current) {
                    throw std::runtime_error("Failed to get current node");
                }
                Iterator Temp(Current, Mutex);
                Current = Current->Next;
                return Temp;
            }

            // 重载--前缀操作符
            Iterator& operator--() {
                std::lock_guard<std::mutex> Lock(*Mutex);
                if (!Current) {
                    throw std::runtime_error("Failed to get current node");
                }
                Current = Current->Prev;
                return *this;
            }

            // 重载--后缀操作符
            Iterator operator--(int) {
                std::lock_guard<std::mutex> Lock(*Mutex);
                if (!Current) {
                    throw std::runtime_error("Failed to get current node");
                }
                Iterator Temp(Current, Mutex);
                Current = Current->Prev;
                return Temp;
            }

            // 重载==操作符
            bool operator==(const Iterator& Other) const {
                return Current == Other.Current;
            }

            // 重载==操作符
            bool operator==(Iterator&& Other) const {
                return Current == Other.Current;
            }

            // 重载!=操作符
            bool operator!=(const Iterator& Other) const {
                return Current != Other.Current;
            }

            // 重载!=操作符
            bool operator!=(Iterator&& Other) const {
                return Current != Other.Current;
            }

            // 重载=操作符
            Iterator& operator=(const Iterator&) = delete;
            Iterator& operator=(Iterator&&) = delete;
        };

        // 构造函数
        ThreadSafeDoublyLinkedList() : Head(nullptr), Tail(nullptr), Mutex() {}

        // 析构函数
        ~ThreadSafeDoublyLinkedList() {
            clear();  // 在析构函数中调用清理方法
        }

        // 添加元素到链表末尾
        void push_back(T Value) {
            std::lock_guard<std::mutex> Lock(Mutex);
            Element* NewNode = new Element(Value);
            if (!Tail) {
                Head = Tail = NewNode;
            }
            else {
                Tail->Next = NewNode;
                NewNode->Prev = Tail;
                Tail = NewNode;
            }
        }

        // 移除第一个元素
        void pop_front() {
            return clear(1);
        }

        // 清理链表
        void clear(int Number = -1) {
            std::lock_guard<std::mutex> Lock(Mutex);
            Element* Next;
            Element* Current = Head;
            while (Number-- && Current) {
                Next = Current->Next;
                delete Current;
                Current = Next;
            }
            if (Current) {
                Head = Current;
                Head->Prev = nullptr;
            }
            else {
                Head = Tail = nullptr;
            }
        }

        // 返回链表的起始迭代器
        Iterator begin() const {
            std::lock_guard<std::mutex> Lock(Mutex);
            return Iterator(Head, &Mutex);
        }

        // 返回链表的结束迭代器
        Iterator end() const {
            return Iterator(nullptr, &Mutex);
        }

        // 返回链表的反向起始迭代器
        Iterator rbegin() const {
            std::lock_guard<std::mutex> Lock(Mutex);
            return Iterator(Tail, &Mutex);
        }

        // 返回链表的反向结束迭代器
        Iterator rend() const {
            return Iterator(nullptr, &Mutex);
        }

    private:
        Element* Head;
        Element* Tail;
        mutable std::mutex Mutex;
    };
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值