复制代码

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

 

posted @ 2018-10-07 21:23  pg633  阅读(263)  评论(0编辑  收藏  举报