gpt4 book ai didi

c++ - 如何在boost进程间构建vector of vector

转载 作者:行者123 更新时间:2023-11-30 04:45:40 29 4
gpt4 key购买 nike

我是新的 boost interprocess,我已经阅读了 Creating vectors in shared memory 上的快速指南.但是这个例子只构造了一个vector<int> ,在我的用例中,我必须构建更复杂的数据结构(通常是嵌套容器)。

我们以vector<vector<int>>为例, 我根据那个快速指南写了一个小例子

#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <string>
#include <cstdlib> //std::system
#include <iostream>

using namespace boost::interprocess;

//Define an STL compatible allocator of ints that allocates from the managed_shared_memory.
//This allocator will allow placing containers in the segment
typedef allocator<int, managed_shared_memory::segment_manager> ShmemAllocator;
typedef allocator<vector<int , ShmemAllocator>, managed_shared_memory::segment_manager> ShmemAllocator2D;

//Alias a vector that uses the previous STL-like allocator so that allocates
//its values from the segment
typedef vector<int, ShmemAllocator> MyVector;
typedef vector<MyVector, ShmemAllocator2D> My2DVector;

//Main function. For parent process argc == 1, for child process argc == 2
int main(int argc, char *argv[])
{
if(argc == 1){ //Parent process
//Remove shared memory on construction and destruction
struct shm_remove
{
shm_remove() { shared_memory_object::remove("MySharedMemory"); }
~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
} remover;

//Create a new segment with given name and size
managed_shared_memory segment(create_only, "MySharedMemory", 65536);

//Initialize shared memory STL-compatible allocator
const ShmemAllocator2D alloc_inst (segment.get_segment_manager());

//Construct a vector named "MyVector" in shared memory with argument alloc_inst
My2DVector *myvector = segment.construct<My2DVector>("MyVector")(alloc_inst);

for (int i = 0; i < 10;++i) {
myvector->emplace_back();
// for (int j = i; j < 10;++j) {
// myvector->back().push_back(j);
// }
}

//Launch child process
std::string s(argv[0]); s += " child ";
if(0 != std::system(s.c_str()))
return 1;

//Check child has destroyed the vector
if(segment.find<MyVector>("MyVector").first)
return 1;
}
else{ //Child process
//Open the managed segment
managed_shared_memory segment(open_only, "MySharedMemory");

//Find the vector using the c-string name
My2DVector *myvector = segment.find<My2DVector>("MyVector").first;

//Use vector in reverse order
for (auto &vec : *myvector){
std::cout << "row" << std::endl;
for (auto i:vec)
std::cout << i << ", ";
std::cout << std::endl;
}


//When done, destroy the vector from the segment
segment.destroy<MyVector>("MyVector");
}

return 0;
};

但是这段代码会给出编译错误:

/usr/include/boost/container/vector.hpp:301:54: error: no matching function for call to ‘boost::interprocess::allocator<int, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> >::allocator()’
: Allocator(), m_start(), m_size(), m_capacity()

我想 emplace_back应该像 STL vector 一样工作....

最佳答案

它并不像您想象的那么简单 - 您初始化外部 std::vector与分配器。但是当使用 emplace_back创建内部 std::vector<int>编译器尝试使用默认构造函数(由于使用共享内存分配器而不存在)构造它。我看到了两种可能的解决方案。

显式传递分配器

代替

myvector->emplace_back();

使用

ShmemAllocator2D ac(segment.get_segment_manager());
myvector->emplace_back(ac);

对于向 vector 添加任何内容的每个操作。然而,这很容易出错,而且很麻烦,并且已经有一个已知的解决方案来解决这个问题:

scoped_allocator_adaptor

只需替换:

typedef allocator<vector<int , ShmemAllocator>, managed_shared_memory::segment_manager>  ShmemAllocator2D;

与:

typedef scoped_allocator_adaptor<allocator<vector<int , ShmemAllocator>, managed_shared_memory::segment_manager>>  ShmemAllocator2D;

应该没问题

关于c++ - 如何在boost进程间构建vector of vector,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57151066/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com