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

    template<typename Key, typename Value, typename Hash = std::hash<Key>>
    class ThreadsafeLookupTable
    {
    private:
        class BucketType
        {
        private:
            typedef std::pair<Key, Value> bucketValue;
            typedef std::list<bucketValue> bucketData;
            typedef typename bucketData::iterator bucketIterator;
    
            bucketData                   data;
            mutable boost::shared_mutex mutex;
    
            bucketIterator findEntryFor(Key const& key) const
            {
                return std::find_if(data.cbegin (), data.cend (),
                                    [&](bucketValue const& item)
                                    {return item.first == key;});
            }
        public:
            Value valueFor(Key const& key, Value const& defaultValue) const
            {
                boost::shared_lock<boost::shared_mutex> lock(mutex);
                auto const foundEntry = findEntryFor (key);
                return (foundEntry == data.cend ()? defaultValue : foundEntry->second);
            }
    
            void addOrUpdate(Key const& key, Value const& value)
            {
                std::unique_lock<boost::shared_mutex> lock(mutex);
                auto const foundEntry = findEntryFor (key);
                if (foundEntry == data.cend ()){
                    data.push_back ({key, value});
                }
                else{
                    foundEntry->second = value;
                }
            }
    
            void removeMapping(Key const& key)
            {
                std::unique_lock<boost::shared_mutex> lock(mutex);
                auto const foundEntry = findEntryFor (key);
                if (foundEntry != data.cend ()){
                    data.erase (foundEntry);
                }
            }
        };
    
        std::vector<std::unique_ptr<BucketType>> buckets;
        Hash hasher;
    
        BucketType& getBucket(Key const& key) const
        {
            auto const bucketIdnex = hasher(key) % buckets.size ();
            return *buckets[bucketIdnex];
        }
    
    public:
        ThreadsafeLookupTable(const ThreadsafeLookupTable&) = delete;
        ThreadsafeLookupTable& operator=(const ThreadsafeLookupTable&) = delete;
    
        typedef Key   KeyType;
        typedef Value MappedValue;
    
        ThreadsafeLookupTable(size_t numBuckets = 19, Hash const& hasher_ = Hash()):
            buckets(std::vector<std::unique_ptr<BucketType>>(numBuckets)),
            hasher(hasher_)
        {
            for (size_t i = 0; i < numBuckets; ++i){
                buckets[i].reset(std::make_unique<BucketType>());
            }
        }
    
        Value valueFor(Key const& key, Value const& defaultValue)
        {
            getBucket (key).valueFor (key, defaultValue);
        }
    
        void addOrUpdateMapping(Key const& key, Value const& value)
        {
            getBucket (key).addOrUpdate (key, value);
        }
    
        void removeMapping(Key const& key)
        {
            getBucket (key).removeMapping (key);
        }
    };
     
  • 相关阅读:
    Java练习 SDUT-3328_JAVA判断合法标识符
    问题2:css图片、文字居中
    问题1:jquery实现全选功能,第二次失效(已解决)
    问题2:input、textarea、submit 宽度设置为100%,但显示宽度不一致
    问题1:设置了text-overflow : ellipsis未起作用
    问题1:canvas绘制图片加载不出来
    问题1:鼠标指向导航栏li,但li中a样式未改变
    Shell工具| 流程控制
    Hadoop |集群的搭建
    MySQL高级01
  • 原文地址:https://www.cnblogs.com/wuOverflow/p/4841903.html
Copyright © 2011-2022 走看看