How to allocate complicated structure in shared memory using boost::interprocess::vector?


How to allocate complicated structure in shared memory using boost::interprocess::vector?



I need to allocate vector gates in shared memory using boost::interprocess library. For ints is given clear example in boost documentation. But is it possible to allocate in shared memory structure given below.


typedef struct
{
int outGate;
unsigned int outPin;
int inGate;
unsigned int inPin;
MyClass* data;
} wire;

typedef struct
{
unsigned int gateType;
unsigned int inPins;
unsigned int outPins;
vector<wire> inWires;
vector<wire> outWires;
} gate;

vector<gate> gates;



Thank you.





You do know that with C++ you don't really need typedef for simple structures? Structures are just like classes, and the structure name itself can be used as a type.
– Some programmer dude
Jan 23 '14 at 12:17


typedef





You need allocators that work with shared memory. For this to work you need to be using C++11, or alternatively use boost::container::vector instead of std::vector.
– n.m.
Jan 23 '14 at 14:12


boost::container::vector


std::vector





The example uses int, but you can replace every occurrence of int in the example with wire and it will work just as well.
– Caleth
Jul 3 at 8:54


int


int


wire




1 Answer
1



//header.h file looks like the below snippet


typedef struct
{
int outGate;
unsigned int outPin;
int inGate;
unsigned int inPin;
} wire;

typedef struct
{
unsigned int gateType;
unsigned int inPins;
unsigned int outPins;
std::vector<wire> inWires;
std::vector<wire> outWires;
} gate;

std::vector<gate> gates;
wire wiredata;
gate gatedata;



sender.cpp looks like the following snippet


#include "boost.h"
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/allocators/allocator.hpp>

int main()
{
using namespace boost::interprocess;
try {
shared_memory_object::remove("MySharedMemory");
//create the shared memory
managed_shared_memory segment(create_only, "MySharedMemory", 65536);

//create the allocators for the struct elements to be accessed as vectors
typedef allocator<gate, managed_shared_memory::segment_manager>gate_alloc;
typedef allocator<wire, managed_shared_memory::segment_manager>inwire_alloc;
typedef allocator<wire, managed_shared_memory::segment_manager>outwire_alloc;

//create a boost vector with an associated allocator to it
typedef vector<gate, gate_alloc>gate_vec;
typedef vector<wire, inwire_alloc>inwire_vec;
typedef vector<wire, outwire_alloc>outwire_vec;

//Initialize shared memory STL-compatible allocator
const gate_alloc alloc_inst(segment.get_segment_manager());
const inwire_alloc alloc_inst1(segment.get_segment_manager());
const outwire_alloc alloc_inst2(segment.get_segment_manager());

//construct the segment for pushing the data into it
gate_vec *gate_data = segment.construct<gate_vec>("gatedata")(alloc_inst);
inwire_vec *inwire_data = segment.construct<inwire_vec>("inwiredata")
(alloc_inst1);
outwire_vec *outwire_data = segment.construct<outwire_vec>("outwiredata")
(alloc_inst2);


//push the data into the vectors
wiredata.inGate = 10;
wiredata.inPin = 2;
wiredata.outGate = 1;
wiredata.outPin = 3;
inwire_data->push_back(wiredata);
outwire_data->push_back(wiredata);

gatedata.gateType = 1;
gatedata.inPins = 2;
gatedata.outPins = 3;
gate_data->push_back(gatedata);

std::cout << gate_data->at(0).gateType << "n";
}

catch (...) {
shared_memory_object::remove("MySharedMemory");
throw;
}
shared_memory_object::remove("MySharedMemory");
return 0;
}



receiver.cpp looks like the following snippet


#include "boost.h"
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <algorithm>

int main()
{
using namespace boost::interprocess;
try {
//A special shared memory where we can
//construct objects associated with a name.
//Connect to the already created shared memory segment
//and initialize needed resources
managed_shared_memory segment(open_only, "MySharedMemory"); //segment name

typedef allocator<gate, managed_shared_memory::segment_manager>gate_alloc;
typedef allocator<wire, managed_shared_memory::segment_manager>inwire_alloc;
typedef allocator<wire, managed_shared_memory::segment_manager>outwire_alloc;

typedef vector<gate, gate_alloc>gate_vec;
typedef vector<wire, inwire_alloc>inwire_vec;
typedef vector<wire, outwire_alloc>outwire_vec;

//fetch the vector elements from the segment using the segment names
gate_vec *gate_data = segment.find<gate_vec>("gatedata").first;
inwire_vec *inwire_data = segment.find<inwire_vec>("inwiredata").first;
outwire_vec *outwire_data = segment.find<outwire_vec>("outwiredata").first;

std::cout << gate_data->at(0).gateType << "n" << inwire_data->at(0).inGate <<
"n" << outwire_data->at(0).outGate;
segment.destroy<gate_vec>("gatedata");
segment.destroy<inwire_vec>("inwire_vec");
segment.destroy<outwire_vec>("outwire_vec");

}
catch (...) {
shared_memory_object::remove("MySharedMemory");
throw;
}
shared_memory_object::remove("MySharedMemory");
return 0;



}



the above code(sender.cpp) writes the structure data into the shared memory and the receiver.cpp reads the data from the shared memory.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

PHP contact form sending but not receiving emails

PHP parse/syntax errors; and how to solve them?

iOS Top Alignment constraint based on screen (superview) height