如何使用boost :: intercons :: vector在共享内存中分配复杂的结构
How to allocate complicated structure in shared memory using boost::interprocess::vector?
我需要使用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从共享内存中读取数据。
相关文章:
- 将数组的地址分配给变量并删除
- vector.resize()中的分配错误
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- Win32编译器选项和内存分配
- 函数中堆分配的效果与缺少堆分配的情况
- 函数复杂度分析
- 向量 <int> a {N, 0} 和 int arr a[N] = {0} 的时间复杂度有什么区别
- while循环中while循环的时间复杂度是多少
- 使用动态分配的数组会导致代码分析发出虚假的C6386缓冲区溢出警告
- 多个文件的内存分配错误"在抛出 'std :: bad_alloc' what (): std :: bad_alloc 的实例后终止调用" [C++]
- 获取字符串的长度并将其分配给数组
- 将地址分配给本地指针后,公共对象的变量将消失
- 递归模板化函数不能分配给具有常量限定类型"const tt &"的变量"state"
- 重复使用预分配的向量<复杂<double>>作为<double>长度两倍的向量
- 在避免新分配的同时,const变量的复杂初始化
- 如何使用boost :: intercons :: vector在共享内存中分配复杂的结构
- 更适合动态或静态分配复杂的数据成员
- 如何使用复杂构造函数分配对象
- 具有c++标准规定的时间复杂度的操作能否动态分配内存?
- 通过new分配复杂构造函数