【问题标题】:std::list in a boost::interprocess::managed_shared_memorystd::list 在 boost::interprocess::managed_shared_memory
【发布时间】:2019-01-23 19:33:53
【问题描述】:

最近我got schooled 并学会了在boost::interprocess::managed_shared_memory 段中使用unordered_map 的正确方法。到目前为止一切顺利,但我需要添加更多 STL 容器。

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

std::vector

以下代码有效:

#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

列表等效代码导致错误。

#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)和错误如下:

$ 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 仍然存在错误。

【问题讨论】:

    标签: c++ boost stl allocator boost-interprocess


    【解决方案1】:

    并非所有标准库实现都完全支持有状态分配器(还没有?)。

    在这种情况下,您的std::list&lt;&gt; 似乎没有。只需为 Boost Container 选择一个,它也可以通过 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::unordered_map 答案中昨天的 cmets。该解决方案有效——正确编译和插入。
    • 没问题。感谢您发布另一个自我回答的问题,刚刚赞成:)
    • 他们是否全面声称它?如果他们为一个容器而不是另一个容器声明它,这不是一个错误。也许有一个“功能准备就绪”页面可以使承诺更加明确。他们跳过“未经测试”的功能是不是很合理
    • 顺便说一下,另一个常见的障碍是使用非原始指针类型(所以当allocator &lt;T&gt;::pointer_type != T*
    • 正如上面的评论所说,问题不是缺乏对有状态分配器的支持,而是缺乏对“花式指针”的支持,并且已经有一个错误报告:gcc.gnu.org/bugzilla/show_bug.cgi?id=57272
    【解决方案2】:

    [allocator.requirements]第9段:

    分配器可以限制可以实例化的类型以及可以调用其构造成员的参数。如果某个类型不能与特定分配器一起使用,则分配器类或对构造的调用可能无法实例化。

    您的分配器可以拒绝为除 T 之外的任何东西分配内存。这将阻止它在基于节点的容器中使用,例如 std::list 需要分配自己的内部节点类型(不仅仅是容器的value_type) 但它适用于 std::vector

    正如@Jonathan-Wakely 在https://stackoverflow.com/a/28995278/4288486 中发布的那样

    【讨论】:

    • 这不是问题。
    猜你喜欢
    • 2012-05-20
    • 1970-01-01
    • 2014-05-06
    • 1970-01-01
    • 2021-05-19
    • 2017-11-02
    • 2017-05-21
    • 1970-01-01
    • 2021-06-09
    相关资源
    最近更新 更多