本文介绍了std :: list in boost :: interprocess :: managed_shared_memory的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我上学了,并了解了在boost::interprocess::managed_shared_memory段中包含unordered_map的正确方法.到目前为止,一切都很好,但是我将需要添加更多的STL容器.

Recently I got schooled and learnt the proper way of having a unordered_map inside a boost::interprocess::managed_shared_memory segment. So far so good, but I will need to add a few more STL containers.

理想情况下,我希望能够对任何STL容器遵循相同的规则.现在,我需要 std::list .我不能使它工作.不过,我可以使 std::vector 起作用.

Ideally, I would want to be able to follow the same for any STL container. Right now I'm in need of a std::list. I can't make it work. I can make a std::vector work, though.

以下代码有效:

#include <vector>
#include <boost/interprocess/managed_shared_memory.hpp>

namespace ipc = boost::interprocess;
using Segment = ipc::managed_shared_memory;
using Manager = Segment::segment_manager;
template <typename T> using Alloc = ipc::allocator<T, Manager>;
template <typename K> using Vector = std::vector<K, Alloc<K>>;

int main() {
  boost::interprocess::shared_memory_object::remove("test");
  Segment _segment{ipc::create_only, "test", 1ul<<40};
  Manager *mgr = _segment.get_segment_manager();
  Vector<int> *v = _segment.construct<Vector<int>>("v")(mgr);
  v->emplace_back(1);
}

std :: list

等效于列表的代码会导致错误.

std::list

The list-equivalent code results in an error.

#include <list>
#include <boost/interprocess/managed_shared_memory.hpp>

namespace ipc = boost::interprocess;
using Segment = ipc::managed_shared_memory;
using Manager = Segment::segment_manager;
template <typename T> using Alloc = ipc::allocator<T, Manager>;
template <typename K> using List = std::list<K, Alloc<K>>;

int main() {
  boost::interprocess::shared_memory_object::remove("test");
  Segment _segment{ipc::create_only, "test", 1ul<<40};
  Manager *mgr = _segment.get_segment_manager();
  List<int> *v = _segment.construct<List<int>>("v")(mgr);
  v->emplace_back(1);
}

以下是编译标志(使用g ++-7)和错误:

The compilation flags (using g++-7) and error are the following:

$ g++ -std=gnu++17 lol_list.cpp -lrt -pthread -rdynamic -Wfatal-errors && ./a.out && rm ./a.out
In file included from /usr/include/c++/7/list:63:0,
                 from lol_list.cpp:1:
/usr/include/c++/7/bits/stl_list.h: In instantiation of ‘std::__cxx11::list<_Tp, _Alloc>::_Node* std::__cxx11::list<_Tp, _Alloc>::_M_create_node(_Args&& ...) [with _Args = {int}; _Tp = int; _Alloc = boost::interprocess::allocator<int, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> >; std::__cxx11::list<_Tp, _Alloc>::_Node = std::_List_node<int>]’:
/usr/include/c++/7/bits/stl_list.h:1801:32:   required from ‘void std::__cxx11::list<_Tp, _Alloc>::_M_insert(std::__cxx11::list<_Tp, _Alloc>::iterator, _Args&& ...) [with _Args = {int}; _Tp = int; _Alloc = boost::interprocess::allocator<int, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> >; std::__cxx11::list<_Tp, _Alloc>::iterator = std::_List_iterator<int>]’
/usr/include/c++/7/bits/stl_list.h:1133:4:   required from ‘std::__cxx11::list<_Tp, _Alloc>::reference std::__cxx11::list<_Tp, _Alloc>::emplace_back(_Args&& ...) [with _Args = {int}; _Tp = int; _Alloc = boost::interprocess::allocator<int, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> >; std::__cxx11::list<_Tp, _Alloc>::reference = int&]’
lol_list.cpp:16:20:   required from here
/usr/include/c++/7/bits/stl_list.h:578:11: error: cannot convert ‘boost::interprocess::offset_ptr<std::_List_node<int>, long int, long unsigned int, 0>’ to ‘std::__cxx11::list<int, boost::interprocess::allocator<int, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> > >::_Node* {aka std::_List_node<int>*}’ in return
    return __p;
           ^~~
compilation terminated due to -Wfatal-errors.

g++-6g++-8甚至clang-6.0clang-5.0仍然存在错误.

The error persists with g++-6 or g++-8 and even with clang-6.0 and clang-5.0.

推荐答案

并非所有标准库实现都完全支持有状态分配器(还可以吗?).

Not all standard library implementations fully support stateful allocators (yet?).

在这种情况下,您的std::list<>似乎没有.只需选择一个用于Boost容器的容器,也可以通过Boost Interprocess头方便地使用它:

In this case it appears that your std::list<> doesn't. Simply opt for the one for Boost Container, which is conveniently also available through Boost Interprocess headers:

#include <boost/interprocess/containers/list.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>

namespace ipc = boost::interprocess;
using Segment = ipc::managed_shared_memory;
using Manager = Segment::segment_manager;
template <typename T> using Alloc = ipc::allocator<T, Manager>;
template <typename K> using List = ipc::list<K, Alloc<K>>;

int main() {
    boost::interprocess::shared_memory_object::remove("test");
    Segment _segment{ipc::create_only, "test", 1ul<<40};
    Manager *mgr = _segment.get_segment_manager();
    List<int> *v = _segment.construct<List<int>>("v")(mgr);
    v->emplace_back(1);
}

这篇关于std :: list in boost :: interprocess :: managed_shared_memory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 06:15
查看更多