std::list 在 boost::interprocess::managed_shared_memory
std::list in a boost::interprocess::managed_shared_memory
最近我 学会了在 boost::interprocess::managed_shared_memory
段中包含 unordered_map
的正确方法。到目前为止一切顺利,但我需要再添加几个 STL 容器。
理想情况下,我希望能够对任何 STL 容器进行相同的操作。不过,现在我需要一份 std::list
. I can't make it work. I can make a std::vector
工作。
std::向量
以下代码有效:
#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++-6
或 g++-8
甚至 clang-6.0
和 clang-5.0
。
并非所有标准库实现都完全支持有状态分配器(还没有?)。
在这种情况下,您的 std::list<>
似乎没有。只需选择用于 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);
}
[allocator.requirements] paragraph 9:
An allocator may constrain the types on which it can be instantiated and the arguments for which its construct member may be called. If a type cannot be used with a particular allocator, the allocator class or the call to construct may fail to instantiate.
您的分配器可以拒绝为除 T 以外的任何内容分配内存。这将阻止它在基于节点的容器中使用,例如 std::list,这些容器需要分配自己的内部节点类型(不是只是容器的 value_type) 但它适用于 std::vector
@Jonathan-Wakely 在 中发布
最近我 boost::interprocess::managed_shared_memory
段中包含 unordered_map
的正确方法。到目前为止一切顺利,但我需要再添加几个 STL 容器。
理想情况下,我希望能够对任何 STL 容器进行相同的操作。不过,现在我需要一份 std::list
. I can't make it work. I can make a std::vector
工作。
std::向量
以下代码有效:
#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++-6
或 g++-8
甚至 clang-6.0
和 clang-5.0
。
并非所有标准库实现都完全支持有状态分配器(还没有?)。
在这种情况下,您的 std::list<>
似乎没有。只需选择用于 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);
}
[allocator.requirements] paragraph 9:
An allocator may constrain the types on which it can be instantiated and the arguments for which its construct member may be called. If a type cannot be used with a particular allocator, the allocator class or the call to construct may fail to instantiate.
您的分配器可以拒绝为除 T 以外的任何内容分配内存。这将阻止它在基于节点的容器中使用,例如 std::list,这些容器需要分配自己的内部节点类型(不是只是容器的 value_type) 但它适用于 std::vector
@Jonathan-Wakely 在 中发布