首页 > 代码库 > 基于boost实现的共享内存版HashMap
基于boost实现的共享内存版HashMap
#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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。