C++ 并发实战之线程安全线性查找表

 

基于锁的线程安全线性查找表

#include <iostream>
#include <iterator>
#include <vector>
#include <list>
#include <mutex>
#include <algorithm>
#include <boost/thread/shared_mutex.hpp> 
#include <memory>

template<typename Key,typename Value ,typename Hash=std::hash<Key>() > 
class threadsafe_lookup_table{ 
    class table_type
    {
        typedef std::pair<Key,Value> table_node;
        typedef std::list<table_node> table_list;
        typedef typename table_list::iterator table_item;
        table_list data;
        mutable boost::shared_mutex m;
        table_item find_entry_for (Key const & key)
        {
              return std::find_if(data.begin(),data.end(),
                 [&]( table_node const& item){ return item.first ==key;});
        }
        public:
        Value  value_for(Key const &  key,Value const &  value)  
        {
            boost::shared_lock<boost::shared_mutex> lk(m);
            table_item entry = find_entry_for(key);
            return entry==data.end() ? 
                value : entry->second;
        }
        void add_or_update_table(Key const &  key,Value const & value) 
        {
             std::unique_lock<boost::shared_mutex> lk(m);
            table_item entry = find_entry_for(key);
            if(entry == data.end())
            {
                data.push_back(table_node(key,value));
            }
            else
            {
                entry->second= value;  
            }
        }
        void remove_table(Key const & key) 
        {
             std::unique_lock<boost::shared_mutex> lk(m);
            table_item entry = find_entry_for(key);
            if(entry != data.end())
            {
                data.erare(entry);
            }
        }
    };
private:
    std::vector<std::unique_ptr<table_type>> buckets;
    Hash hasher;
public:
    table_type& get_bucket(Key const & key) const
    { 
        return *buckets[hasher(key)% (buckets.size())];
    }
public:
    typedef Key key_type;
    typedef Value value_type;
    typedef Hash hsh_type;
    threadsafe_lookup_table(unsigned bucket_size = 19,Hash   const& hasher_ = Hash())
        :buckets(bucket_size),hasher(hasher_)
    {
        for(unsigned i = 0;i<bucket_size;++i)
            buckets[i].reset(new table_type);
    }
    threadsafe_lookup_table(threadsafe_lookup_table const & ) =delete;
    threadsafe_lookup_table& operator=(threadsafe_lookup_table const & ) =delete;
    Value value_for(Key const&  key,Value const&  value =Value()) const  
    { 
        return get_bucket(key).value_for(key,value);
    }
    void add_or_update_table(Key const &  key,Value const & value) 
    {
        return get_bucket(key).add_or_update_table(key,value);
    }  
    void remove_table(Key const & key) 
    {
        return get_bucket(key).remove_table(key);
    }     
};




 int main()
 {
    threadsafe_lookup_table<int,int,std::hash<int>> x ;
     auto a = x.value_for(1);
     x.add_or_update_table(1,11);

  //  std::cout<<m.value_for(1)<<std::endl;


     return 0;
 }
View Code

 

转载于:https://www.cnblogs.com/corx/p/9751556.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值