程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 基於boost實現的共享內存版HashMap

基於boost實現的共享內存版HashMap

編輯:C++入門知識

基於boost實現的共享內存版HashMap


#include 
#include 
#include 
#include 
#include 

int main (int argc, char *argv[])
{
    typedef int KeyType;
    typedef float MappedType;
    typedef std::pair ValueType;
    typedef boost::interprocess::allocator ShmAlloc;
    typedef boost::unordered_map, std::equal_to, ShmAlloc> ShmHashMap;

    boost::interprocess::managed_shared_memory segment(boost::interprocess::open_or_create, "ContainerSharedMemory", 65536);
    ShmHashMap *hash_map1 = segment.find_or_construct("ShmHashMap1")(3, boost::hash(), std::equal_to(), segment.get_allocator());    
    ShmHashMap *hash_map2 = segment.find_or_construct("ShmHashMap2")(3, boost::hash(), std::equal_to(), segment.get_allocator());    

    for(int i = 0; i < 5; ++i) {
        ShmHashMap::iterator iter = hash_map1->find(i);
        if (iter != hash_map1->end()) {
            std::cout << "[ShmHashMap1]<" << i << ", " << iter->second << ">" << std::endl;
            iter->second += 1.0;
        }   
        hash_map1->insert(ValueType(i, (MappedType)i));
    }   
    for(int i = 0; i < 5; ++i) {
        ShmHashMap::iterator iter = hash_map2->find(i);
        if (iter != hash_map2->end()) {
            std::cout << "[ShmHashMap2]<" << i << ", " << iter->second << ">" << std::endl;
            iter->second += 2.0;
        }   
        hash_map2->insert(ValueType(i, (MappedType)i));
    }   

    return 0;
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved