首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在boost进程中构造向量的向量

如何在boost进程中构造向量的向量
EN

Stack Overflow用户
提问于 2019-07-23 01:32:58
回答 1查看 250关注 0票数 2

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

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

代码语言:javascript
复制
#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;
};

然而,这段代码会给出编译错误:

代码语言:javascript
复制
/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一样工作。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-07-23 05:27:35

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

显式传递分配器

而不是

代码语言:javascript
复制
myvector->emplace_back();

使用

代码语言:javascript
复制
ShmemAllocator2D ac(segment.get_segment_manager());
myvector->emplace_back(ac);

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

scoped_allocator_adaptor

只需替换:

代码语言:javascript
复制
typedef allocator<vector<int , ShmemAllocator>, managed_shared_memory::segment_manager>  ShmemAllocator2D;

通过以下方式:

代码语言:javascript
复制
typedef scoped_allocator_adaptor<allocator<vector<int , ShmemAllocator>, managed_shared_memory::segment_manager>>  ShmemAllocator2D;

而且它应该工作得很好

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57151066

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档