将向量存储在内存映射文件中 [英] Storing vector in memory mapped file

查看:19
本文介绍了将向量存储在内存映射文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将任意元素的向量存储在内存映射文件中(现在我正在尝试使用整数向量成功,但它应该可以使用任意对象的向量).我找到了很多关于使用共享内存这样做的文档,但没有正确使用内存映射文件.因为我已经成功地在内存映射文件中制作并使用了 R 树(比如在 那个例子),我试图用向量复制这个过程,但我想我错过了一些关键元素,因为它不起作用.这是我的代码:

I am attempting to store a vector of arbitrary elements in a memory mapped file (for now I'm trying to succeed with a vector of ints but it should work with vector of arbitrary objects). I have found plenty of documentation on doing so with shared memory, but not with memory mapped files proper. Since I have successfully made and used R-trees in memory mapped file (like in that example), I tried to replicate the process with vectors, but I guess I am missing some crucial element because it doesn't work. Here is my code:

namespace bi = boost::interprocess;
typedef bi::allocator<std::vector<int>, bi::managed_mapped_file::segment_manager> allocator_vec;
std::string vecFile = "/path/to/my/file/vector.dat";
bi::managed_mapped_file file_vec(bi::open_or_create,vecFile.c_str(), 1000);
allocator_vec alloc_vec(file_vec.get_segment_manager());
std::vector<int> * vecptr = file_vec.find_or_construct<std::vector<int> >("myvector")(alloc_vec);

可能我的最后一行是错误的,因为alloc_vec"是作为参数传递给向量构造函数的,这并不期望它(我得到了错误/usr/include/c++/4.8/bits/stl_vector.h:248:7:注意:候选人需要0个参数,1个提供).但是,我不知道如何将分配器传递给 find_or_construc(),我认为这对于在内存映射文件中正确创建向量至关重要.删除最后一行末尾的 (alloc_vec) 会导致另一个错误,我更难解决:

Probably my last line is wrong, because "alloc_vec" is passed as an argument to the vector constructor, which doesn't expect it (I get among others the error /usr/include/c++/4.8/bits/stl_vector.h:248:7: note: candidate expects 0 arguments, 1 provided). However, I don't know then how to pass the allocator to find_or_construc(), which I assume is crucial for the vector to be properly created in the memory mapped file. Removing (alloc_vec) at the end of the last line leads to another error that I have more trouble to solve :

error: cannot convert ‘boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index>::construct_proxy<std::vector<int> >::type {aka boost::interprocess::ipcdetail::named_proxy<boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index>, std::vector<int>, false>}’ to ‘std::vector<int>*’ in initialization
std::vector<int> * vecptr = file_vec.find_or_construct<std::vector<int> >("myvector");

任何帮助将不胜感激.`

Any help will be greatly appreciated.`

推荐答案

如示例所示,将您的自定义分配器告诉向量类,而不是

Like the samples show, tell the vector class about your custom allocator, so instead of

typedef std::vector<int>  MyVec;
MyVec * vecptr = file_vec.find_or_construct<MyVec>("myvector")(alloc_vec);

使用

typedef bi::allocator<int, bi::managed_mapped_file::segment_manager> int_alloc;
typedef std::vector<int, int_alloc>  MyVec;

int_alloc alloc(file_vec.get_segment_manager());
MyVec * vecptr = file_vec.find_or_construct<MyVec>("myvector")(alloc);

注意

  • vector 使用元素类型的分配器(不是用于向量;segment_manager 分配)
  • 由于allocator<>的构造函数是隐式的,你也可以只传递segment_manager:
  • vector uses an allocator for the element types (not for the vector; segment_manager allocates that)
  • since the constructor for allocator<> is implicit, you can also just pass the segment_manager:

在 Coliru 上直播

#include <boost/interprocess/managed_mapped_file.hpp>

namespace bi = boost::interprocess;

int main() {
    std::string vecFile = "vector.dat";
    bi::managed_mapped_file file_vec(bi::open_or_create,vecFile.c_str(), 1000);

    typedef bi::allocator<int, bi::managed_mapped_file::segment_manager> int_alloc;
    typedef std::vector<int, int_alloc>  MyVec;

    MyVec * vecptr = file_vec.find_or_construct<MyVec>("myvector")(file_vec.get_segment_manager());

    vecptr->push_back(rand());
}

这篇关于将向量存储在内存映射文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆