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