1 //////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2006-2009. Distributed under the Boost
4 // Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 // See http://www.boost.org/libs/interprocess for documentation.
9 //////////////////////////////////////////////////////////////////////////////
10 #include <boost/interprocess/detail/config_begin.hpp>
11 #include <boost/interprocess/detail/workaround.hpp>
13 #include <boost/interprocess/managed_shared_memory.hpp>
14 #include <boost/interprocess/allocators/allocator.hpp>
16 #include <boost/unordered_map.hpp> //boost::unordered_map
17 #include <functional> //std::equal_to
18 #include <boost/functional/hash.hpp> //boost::hash
20 #include "../test/get_process_id_name.hpp"
25 using namespace boost::interprocess;
26 //Remove shared memory on construction and destruction
31 shm_remove() { shared_memory_object::remove(test::get_process_id_name()); }
32 ~shm_remove(){ shared_memory_object::remove(test::get_process_id_name()); }
35 shm_remove() { shared_memory_object::remove("MySharedMemory"); }
36 ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
42 //Create shared memory
45 managed_shared_memory segment(create_only, test::get_process_id_name(), 65536);
48 managed_shared_memory segment(create_only, "MySharedMemory", 65536);
53 //Note that unordered_map<Key, MappedType>'s value_type is std::pair<const Key, MappedType>,
54 //so the allocator must allocate that pair.
56 typedef float MappedType;
57 typedef std::pair<const int, float> ValueType;
59 //Typedef the allocator
60 typedef allocator<ValueType, managed_shared_memory::segment_manager> ShmemAllocator;
62 //Alias an unordered_map of ints that uses the previous STL-like allocator.
63 typedef boost::unordered_map
64 < KeyType , MappedType
65 , boost::hash<KeyType> ,std::equal_to<KeyType>
69 //Construct a shared memory hash map.
70 //Note that the first parameter is the initial bucket count and
71 //after that, the hash function, the equality function and the allocator
72 MyHashMap *myhashmap = segment.construct<MyHashMap>("MyHashMap") //object name
73 ( 3, boost::hash<int>(), std::equal_to<int>() //
74 , segment.get_allocator<ValueType>()); //allocator instance
76 //Insert data in the hash map
77 for(int i = 0; i < 100; ++i){
78 myhashmap->insert(ValueType(i, (float)i));
83 #include <boost/interprocess/detail/config_end.hpp>