zoukankan      html  css  js  c++  java
  • 使用引用计数方法管理内存的无锁栈 I

    template<typename T>
    class LockFreeStack
    {
    private:
        struct Node
        {
            std::shared_ptr<T> data;
            Node*              next;
            Node(T const& value):
                data(std::make_shared<T>(value))
            {}
        };
        
        std::atomic<Node*>  head;
        std::atomic<size_t> threadsInPopCount;
        std::atomic<Node*>  toBeDeletedChainHead;
        
        static void deleteNodes(Node* nodes)
        {
            while(nodes != nullptr){
                auto const next = nodes->next;
                delete nodes;
                nodes = next;
            }
        }
        
        void tryReclaim(Node* oldHead)
        {
            // If only this thread in the pop.
            if (threadsInPopCount == 1) {
                auto toBeDeletedList = toBeDeletedChainHead.exchange(nullptr);
                // Now, if only this thread in the pop.
                if (--threadsInPopCount == 0){
                    deleteNodes(toBeDeletedList);
                }
                // If some nodes to be deleted still waiting.
                else if (toBeDeletedList != nullptr){
                    pushListToDelChain(toBeDeletedList);
                }
                delete oldHead;
            }
            else{
                // If more than one thread in the pop.
                pushNodeToDelChain(oldHead);
                --threadsInPopCount;
            }
        }
        
        void pushListToDelChain(Node* nodes)
        {
            auto last = nodes;
            while (auto const next = last->next) {
                last = next;
            }
            pushListToDelChain(nodes, last);
        }
        
        void pushListToDelChain(Node* first, Node* last)
        {
            last->next = toBeDeletedChainHead;
            // Put current head of delete chain to the next of the last.
            // Then let the first be the head.
            while(!toBeDeletedChainHead.compare_exchange_weak(last->next, first));
        }
        
        void pushNodeToDelChain(Node* node)
        {
            pushListToDelChain(node, node);
        }
    public:
        void push(T const& value)
        {
            auto const newNode = new Node(value);
            newNode->next = head.load();
            while (!head.compare_exchange_weak(newNode, newNode->next));
        }
        
        std::shared_ptr<T> pop()
        {
            ++threadsInPopCount;
            auto oldHead = head.load();
            while(oldHead
                  && !head.compare_exchange_weak(oldHead, oldHead->next));
            
            std::shared_ptr<T> result;
            if (oldHead){
                result.swap(oldHead->data);
            }
            tryReclaim(oldHead);
            return result;
        }
        
    };
  • 相关阅读:
    AMQP 介绍
    JMS 介绍
    SpringCloud 入门实战(12)--Zipkin(2)--安装使用
    SpringCloud 入门实战(11)--Zipkin(1)--简介
    Kafka 入门实战(3)--SpringBoot 整合 Kafka
    跨域访问方法介绍(9)--nginx 反向代理
    跨域访问方法介绍(8)--使用 WebSocket
    个人网站建好啦!
    Spring Boot配置Slf4j + log4j2
    SpringBoot 解决跨域,3种解决方案
  • 原文地址:https://www.cnblogs.com/wuOverflow/p/4848436.html
Copyright © 2011-2022 走看看