I would like to place a struct in a shared memory using Boost.Interprocess. This struct will be shared between two processes as a main exchange zone for serialized data. I am currently searching for the best solution in term of simplicity.
namespace bi = boost::interprocess;
typedef bi::allocator<SimpleData, bi::managed_shared_memory::segment_manager> ShmemSimpleDataAllocator;
typedef bi::vector<SimpleData, ShmemSimpleDataAllocator> SimpleDataVector;
struct SimpleData
{
SimpleData() {}
SimpleData(char* ptr)
:data(ptr)
{}
/** Offset pointer to the data */
bi::offset_ptr<char> data;
};
// Main shared structure
struct SharedMainStruct // EDITED, thanks Sehe
{
SharedMainStruct(ShmemSimpleDataAllocator simple_allocator)
:m_inputs(simple_allocator),
m_outputs(simple_allocator)
{}
// Inputs, outputs and properties
SimpleDataVector m_inputs;
SimpleDataVector m_outputs;
bi::interprocess_mutex m_mutex;
bi::interprocess_condition m_cond;
};
// Main code here
// Create managed shared memory
my_sho= new bi::managed_shared_memory (bi::create_only, "test_SO", 1000000);
// Build main structure in shared memory
ShmemSimpleDataAllocator data_allocator(my_sho->get_segment_manager());
m_main_struct = my_sho->construct<SharedMainStruct>("main")(data_allocator); // (1)
The idea is to place this SharedMainStruct
in shared memory so that it is accessible for both processes. Is this code line (1) correct ? I would guess that I miss some runtime code like in the documentation right ? Like :
//Initialize shared memory STL-compatible allocator
const ShmemSimpleDataAllocatoralloc_inst (segment.get_segment_manager());
//Construct a vector named "MyVector" in shared memory with argument alloc_inst
SimpleDataVector* myvector = segment.construct<SimpleDataVector>("SimpleDataVector")(alloc_inst);
-- UPDATE --
Yes, Vectors can be stored as member variables. We should provide allocators as parameters of the constructor...