如何使用boost :: interprocess :: vector在共享内存中分配复杂的结构?

时间:2022-08-25 09:05:49

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.

我需要使用boost :: interprocess库在共享内存中分配矢量门。对于ints,在boost文档中给出了明确的示例。但是可以在下面给出的共享内存结构中进行分配。

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.

谢谢。

1 个解决方案

#1


0  

//header.h file looks like the below snippet

//header.h文件如下面的代码段所示

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

sender.cpp看起来像以下代码段

#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

receiver.cpp看起来像下面的代码片段

#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.

上面的代码(sender.cpp)将结构数据写入共享内存,receiver.cpp从共享内存中读取数据。

#1


0  

//header.h file looks like the below snippet

//header.h文件如下面的代码段所示

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

sender.cpp看起来像以下代码段

#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

receiver.cpp看起来像下面的代码片段

#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.

上面的代码(sender.cpp)将结构数据写入共享内存,receiver.cpp从共享内存中读取数据。