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

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

本文关键字:分配 复杂 内存 结构 boost 何使用 intercons vector 共享      更新时间:2023-10-16

我需要使用boost :: internocess库中的共享内存中分配vector 。对于INT,在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;

谢谢。

//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看起来像以下片段

#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看起来像以下片段

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

}

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