码迷,mamicode.com
首页 > 编程语言 > 详细

线程安全的链表

时间:2015-09-29 09:49:25      阅读:237      评论:0      收藏:0      [点我收藏+]

标签:

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);
            }
        }
    }
};

 

线程安全的链表

标签:

原文地址:http://www.cnblogs.com/wuOverflow/p/4845515.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!