I’ve the following code pattern:
class A {
double a, b, c;
...
};
class B {
map<int, A> table; // Can have maximum of MAX_ROWS elements.
...
};
class C {
B entries;
queue<int> d;
queue<int> e;
...
};
Now I want to store an object of type C in a shared memory, so that different processes can append, update and read it. How can I do this? (Note: I know how to store a simple C array that has a fixed size in shared memory. Also, remember that B.table may have arbitrary entries.
Use boost::interprocess, this library exposes this functionality.
EDIT: Here are some changes you’ll need to do:
The example already defines an allocator that will allocate from the shared memory block, you need to pass this to the
mapand thequeue. This means you’ll have to change your definitions:For
queue, this is slightly complicated, because of the fact that it’s really just an adapter, so you need to pass in the real implementation class as a template parameter:Now your class
Cchanges slightly:Now from the segment manager, construct an instance of
Cwith the allocator.I think that should do the trick. NOTE: You will need to provide two allocators (one for
queueand one formap), not sure if you can construct two allocators from the same segment manager, but I don’t see why not.