HashMap的简单实现,具有线程安全

来源:互联网 发布:软件开发 详细设计 编辑:程序博客网 时间:2024/04/27 21:06

至于hashMap的原理,我想不必多说,大家都清楚,直接上代码啦

HashMap.h

#ifndef HashMap_H#define HashMap_H#include "iostream"#include "boost/thread/mutex.hpp"#include "boost/thread/shared_mutex.hpp"const int HM_SIZE=200;struct Node{    int key;    int value;    Node* next;    Node(int k,int v):key(k),value(v),next(NULL)    {    }};class HashMap{public:    HashMap();    ~HashMap();    void insert(int key,int value);    void remove(int key);    Node* find(int key);    size_t size();private:    int hash(int key) const;private:    size_t m_nSize;    Node** m_map;    boost::shared_mutex m_rwMutex[HM_SIZE/5];//共享互斥信号量 };#endif;

HashMap.cpp

#include "HashMap.h"HashMap::HashMap() :m_nSize(0){    m_map=new Node*[HM_SIZE];    for(int i=0;i<HM_SIZE;i++)    {        m_map[i]=NULL;    }}HashMap::~HashMap(){    for(int i=0;i<HM_SIZE;i++)    {        Node* p=NULL;        while(m_map[i])        {            p=m_map[i];            m_map[i]=m_map[i]->next;            delete p;        }    }    delete[] m_map;}//插入void HashMap::insert(int key,int value){    Node* p=find(key);    if(p)    {        p->value=value;        return;    }    int index = hash(key);    Node* pNode=new Node(key,value);    boost::unique_lock<boost::shared_mutex> wLock(m_rwMutex[index/5]);//写锁,分桶    p=m_map[index];    while(p)    {        if(p->next==NULL)break;        p=p->next;    }    if(p)p->next=pNode;    else m_map[index] = pNode;    m_nSize++;}//删除void HashMap::remove(int key){    int index = hash(key);    Node* p = m_map[index];    Node* last=NULL;    boost::unique_lock<boost::shared_mutex> wLock(m_rwMutex[index/5]);//写锁,分桶    while(p)    {        if(p->key==key)        {            if(last)            {                last->next=p->next;            }            else            {                m_map[index] = p->next;            }            delete p;            m_nSize--;            break;        }        last=p;        p=p->next;    }}//查找Node* HashMap::find(int key){    int index=hash(key);    boost::shared_lock<boost::shared_mutex> rLock(m_rwMutex[index/5]);//读锁,分桶    Node* p=m_map[index];    while(p)    {        if(key==p->key)return p;        p=p->next;    }    return NULL;}//哈希函数int HashMap::hash(int key) const{    return key%HM_SIZE;}//获取容器大小size_t HashMap::size(){    boost::shared_lock<boost::shared_mutex> rLock[HM_SIZE / 5];    for (int i = 0; i < HM_SIZE / 5; i++)    {        boost::shared_lock<boost::shared_mutex> lock(m_rwMutex[i]);        rLock[i].swap(lock);    }    return m_nSize;}

test.cpp

// MyHashMap.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include "HashMap.h"#include <boost/thread.hpp>  #include <boost/shared_ptr.hpp>HashMap myMap;void threadfun(int key,int value){    std::cout << "inserting key:" << key << std::endl;    myMap.insert(key, value);    std::cout << "current size:" << myMap.size() <<"\tfind key-value:"<<key<<"->"<<myMap.find(key)->value<< std::endl;    boost::this_thread::sleep_for(boost::chrono::microseconds(500));//睡眠500毫秒    std::cout << "removing key:" << key << std::endl;    myMap.remove(key);}int _tmain(int argc, _TCHAR* argv[]){    std::cout << "-----------thread create-----------" << std::endl;    const int ThreadCount = 1500;    boost::shared_ptr<boost::thread> thread_array[ThreadCount];    for (int i = 0; i < ThreadCount; i++)    {        thread_array[i].reset(new boost::thread(threadfun,i+1,rand()));    }    for (int i = 0; i < ThreadCount; i++)    {        thread_array[i]->join();    }    system("pause");    return 0;}

上述只是一个简单的hashmap实现,可能有些错误,欢迎指正!

0 0
原创粉丝点击