zoukankan      html  css  js  c++  java
  • 线程安全的链表

    template<typename T>
    class ThreadsafeList
    {
        struct Node
        {
            std::mutex m;
            std::shared_ptr<T> data;
            std::unique_ptr<Node> next;
    
            Node():
                next(nullptr)
            {}
    
            Node(T const& value):
                data(std::make_shared<T>(value))
            {}
        };
    
        Node head;
    
    public:
        ThreadsafeList() = default;
        ThreadsafeList(const ThreadsafeList&) = delete;
        ThreadsafeList& operator=(const ThreadsafeList&) = delete;
        ~ThreadsafeList()
        {
            removeIf([](T const&){return true;});
        }
    
        void pushFront(T const& value)
        {
            auto newNode = std::make_unique<Node>(value);
            std::unique_lock<std::mutex> lock(head.m);
            newNode->next = std::move(head.next);
            head.next = std::move(newNode);
        }
    
        template<typename Function>
        void forEach(Function f)
        {
            Node* current = &head;
            std::unique_lock<std::mutex> lock(head.m);
            while(auto const next = current->next.get ()){
                std::unique_lock<std::mutex> nextLock(next->m);
                lock.unlock ();
                f(*next->data);
                current = next;
                lock = std::move(nextLock);
            }
        }
    
        template<typename Predicate>
        std::shared_ptr<T> findFirstIf(Predicate p)
        {
            Node* current = &head;
            std::unique_lock<std::mutex> lock(head.m);
            while(auto const next = current->next.get ()){
                std::unique_lock<std::mutex> nextLock(next->m);
                lock.unlock ();
                if (p(*next->data)){
                    return next->data;
                }
                current = next;
                lock = std::move(nextLock);
            }
            return std::shared_ptr<T>();
        }
    
        template<typename Predicate>
        void removeIf(Predicate p)
        {
            Node* current = &head;
            std::unique_lock<std::mutex> lock(head.m);
            while(auto const next = current->next.get ()){
                std::unique_lock<std::mutex> nextLock(next->m);
                if (p(*next->data)){
                    auto oldNext = std::move(current->next);
                    current->next = std::move(next->next);
                    nextLock.unlock ();
                }
                else{
                    lock.unlock ();
                    current = next;
                    lock = std::move(nextLock);
                }
            }
        }
    };
  • 相关阅读:
    悼念丹尼斯·里奇,那个给乔布斯提供肩膀的巨人(转载)
    c# 做成Windows服务
    Visual Studio 2010 新建完项目编译就出错
    C#调用Win32 的API函数User32.dll
    c# Remoting小例子
    backgroundworker使用 实现进度条ProgressBar
    winform最小化后隐藏到右下角,单击或双击后恢复
    关于Thread的实例
    c# 捕获的异常写到日志里
    C# delegate and event 规范写法
  • 原文地址:https://www.cnblogs.com/wuOverflow/p/4845515.html
Copyright © 2011-2022 走看看