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;
        }
        
    };
  • 相关阅读:
    MyEclipse 启动报错:'Building workspace' has encountered a problem解决方法
    MyEclipse快捷键大全
    Maven学习 (四) 使用Nexus搭建Maven私服
    PHP 语法
    PHP 在Win下的安装
    PHP 简介
    【CMS】安装CMS
    【CMS】DedeCMS Error: (PHP 5.3 and above) Please set 'request_order' ini value to include C,G and P (recommended: 'CGP') in php.ini
    mysqladmin: connect to server at 'localhost' failed
    解压版MySQL安装后初始化root密码
  • 原文地址:https://www.cnblogs.com/wuOverflow/p/4848436.html
Copyright © 2011-2022 走看看