I’m trying to construct a map in shared memory of the following type
I create the shared memory region like this :
managed_shared_memory segment(create_only ,"MyMap" ,size);
ShMemAllocator_t alloc_inst (segment.get_segment_manager());
map = segment.construct<MyMap_t>("MyMap")
(std::less<int>()
,alloc_inst);
The values in the map are as follows:
typedef pair<MutexType, boost::interprocess::offset_ptr<void> > ValueType ;
MutexType is itself a structure containing a read and a write mutex (using read_lock and write_lock) ;
defined as follows:
typedef struct mutex_struct{
sharable_lock<interprocess_mutex> read_lock(interprocess_mutex, defer_lock);
scoped_lock<interprocess_mutex> write_lock(interprocess_mutex, defer_lock);
} MutexType;
“size” is the total size of the map (in terms of objects, so the sum of the data size pointed to by all the void pointers).
How can I ensure that this void* data is also located in this memory segment I created, how do I instantiate it within the existing shared memory region). The reason for doing this is that I want to allocate this large buffer once only but repeatedly removing/adding objects to it (the map models a cache) I have yet to find a way in which multiple objects can be allocated within the same memory segment within a map. Furthermore, seeking to allocate the MutexType pair returns a compilation error stating that no “call” operator is provided.
You are basically already there. Call whatever object type that you are allocating in shared memory
SecondValue_t. Instead ofShMemAllocator_t, define a different interprocess allocator type, saySecondValueAllocator_t, for allocating theSecondValue_tobjects. Whenever you want to insert aValueTypeobject into the map, the second value of theValueTypeobject is allocated with theSecondValueAllocator_tinstance.Here is a complete example, partly using the code in my answer for Interprocess reader/writer lock with Boost:
Test it out by starting the parent process first:
Then some children:
Sample output: