码迷,mamicode.com
首页 > 其他好文 > 详细

基于boost实现的共享内存版HashMap

时间:2015-01-05 20:32:30      阅读:511      评论:0      收藏:0      [点我收藏+]

标签:c++   boost   hashmap   sharedmemory   

#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/unordered_map.hpp>
#include <boost/functional/hash.hpp>
#include <functional>

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

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

    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;
}

基于boost实现的共享内存版HashMap

标签:c++   boost   hashmap   sharedmemory   

原文地址:http://blog.csdn.net/cszhouwei/article/details/42427607

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!