我想使用boost进程共享对象的向量 . 对象来自以下结构:
struct Foo
{
int id;
float time;
bool isFoo;
float myArray[7];
std::vector<int> vectorData;
};
我正在创建boost进程间分配器和进程间向量:
typedef allocator<Foo, managed_shared_memory::segment_manager> FooAllocator;
typedef std::vector<Foo, FooAllocator> FooVector;
在我的main函数中,我基于以下内容初始化内存段,分配器和向量:
boost -> creating vectors in shared memory
所以:
managed_shared_memory mem_segment(open_or_create, "MemShare", 65536);
const FooAllocator alloc_inst(mem_segment.get_segment_manager());
fooVector = mem_segment.find_or_construct<FooVector>("FooVector")(alloc_inst);
现在,这适用于Foo结构中除vector之外的每种数据类型 . 因此,如果我尝试分享这个我从Foo获得所有成员,并且对于矢量数据我得到“未定义的内存位置”我知道std :: vector不能直接共享 . 所以我使用boost :: interprocess:vector创建了新的Foo结构
struct FooInter
{
int id;
float time;
bool isFoo;
float myArray[7];
MyVector* pointcloud;
};
MyVector的地方是:
typedef allocator<int, managed_shared_memory::segment_manager> VectorAllocator;
typedef boost::interprocess::vector<int, VectorAllocator> MyVector;
我正在为MyVector分配内存,
const VectorAllocator vec_alloc_inst(mem_segment.get_segment_manager());
MyVector* tmpVec = mem_segment.construct<MyVector>("MyVector")(vec_alloc_inst);
然后我现在尝试做的是将Foo映射到FooInter . 我在for循环中映射矢量数据:
for (int t = 0; t < foo.vectorData.size()-1; t++) {
tmpVec->push_back(foo.vectorData[t]);
}
然后将tmpVec复制到fooInter.vectorData:
memcpy(fooInter.pointcloud, tmpVec, sizeof(int) * tmpVec->size());
这适用于但不适用于foo.vectorData的整个大小 . 所以它适用于100个项目,但如果我使用foo.vectorData.size()它会返回错误的内存分配 .
有人可以帮我这个 . 我需要知道共享这种类型结构的正确方法 . 我觉得我所做的完全错了 . 也许我需要将矢量序列化为字符串或类似的东西 .
编辑:
基于sehe的答案:
我有类型的对象消息:
struct Foo
{
int id;
float time;
bool isFoo;
float myArray[7];
std::vector<int> pointcloud;
};
我需要在inter_foos中传递该对象 . 所以在sehe的代码中:
int main() {
auto segment = Shared::open();
auto& inter_foos = *segment.find_or_construct<InterFoos>("InterFoos")(segment.get_segment_manager());
// you can directly append to the shared memory vector
int nextid = inter_foos.size();
//instead of this
inter_foos.push_back({++nextid, 0, true, {.1,.2,.3,.4,.5,.6,.7}, Ints ({10,20,30}, segment.get_segment_manager()) });
//i need this
inter_foos.push_back({msg.id, msg.time, true, msg.myArray, Ints (msg.pointcloud, segment.get_segment_manager()) });
//i can't pass msg.poincloud to this object!!!
// or copy from a non-shared vector:
std::vector<Foo> const local {
{++nextid, 0, true, {.1,.2,.3,.4,.5,.6,.7}, {10,20,30} },
{++nextid, 1, true, {.2,.3,.4,.5,.6,.7,.8}, {20,30,40} },
{++nextid, 2, true, {.3,.4,.5,.6,.7,.8,.9}, {30,40,50} },
};
for (auto& local_foo : local)
inter_foos.emplace_back(local_foo);
// print the current contents
for (auto& foo : inter_foos)
std::cout << foo << "\n";
}
2 回答
你可以使用这个方法
making non-shared copies of boost::interprocess shared memory objects
boost::interprocess scoped_allocator AND Containers of containers NOT in shared memory
见这个演示¹
Live On Coliru
打印:
第二轮:
Scoped Allocators
请注意:
是因为支持并通过Boost传递的作用域分配器's container implementations. If you didn'使用它,事情看起来像这样: Live On Coliru
¹使用映射文件,因为COLIRU不支持共享内存
我无法想象使用
memcpy
复制MyVector
会运行良好 . 当然你需要的是:换句话说,构造pointcloud指向的对象,然后插入其中 .