共享内存中的c++ boost icl容器
c++ boost icl containers in shared memory
我与boost::icl::interval_map工作完美,但我希望这个容器存储在共享内存中。boost是否支持在共享内存中存储boost::icl容器
using namespace std;
using namespace boost::icl;
struct IFM {
std::string destinationGroup;
int priority;
IFM()
{
destinationGroup= "";
priority = 0;
}
IFM(const std::string& d, const int& p)
{
destinationGroup= d;
priority = p;
}
IFM& operator +=(const IFM& right)
{
destinationGroup+= right.destinationGroup;
priority += right.priority;
return *this;
}
bool operator <(const IFM& left) const {
if(priority < left.priority)
return true;
}
bool operator ==(const IFM& left) const {
return destinationGroup== left.destinationGroup
&& priority == left.priority;
}
};
typedef std::set<IFM> guests;
void boost_party()
{
interval_map<double, guests> party;
IFM i = {"123", 1};
IFM j = {"124", 1};
IFM k = {"126", 2,};
IFM l = {"128", 1};
IFM m = {"129", 1};
IFM n = {"130", 1};
guests ii;
ii.insert(i);
guests jj;
jj.insert(j);
guests kk;
kk.insert(k);
guests ll;
ll.insert(l);
guests mm;
mm.insert(m);
party.add(make_pair(interval<double>::closed(12345600000,12345699999), guests(ii)));
party.add(make_pair(interval<double>::closed(32100000000,32199999999), guests(jj)));
party.add(make_pair(interval<double>::closed(42000000000,42999999999), guests(ll)));
party.add(make_pair(interval<double>::closed(42101000000,42101099999), guests(kk)));
party.add(make_pair(interval<double>::closed(67000000000,67999999999), guests(mm)));
interval_map<double, guests>::const_iterator it;
it = party.find(42101035898);
if (it != party.end()) {
interval<double>::type when = it->first;
guests who = (*it++).second;
cout << who.size() << endl;
for (auto it2 : who) {
cout << when << ": " << it2.destinationGroup<< endl;
}
}
}
int main() {
boost_party();
return 0;
}
给出以下预期的输出现在我试着把一个简单的映射interval_map在共享内存,但我的代码从来没有编译
boost::interprocess::managed_shared_memory segment(
boost::interprocess::create_only, "MySharedMemory" //segment name
, 65536);
typedef int KeyType;
typedef int MappedType;
typedef pair<int,int> keyvalue;
typedef boost::interprocess::allocator<keyvalue, boost::interprocess::managed_shared_memory::segment_manager>
ShmemAllocator;
ShmemAllocator alloc_inst (segment.get_segment_manager());
typedef boost::icl::interval_map<int, int, boost::icl::partial_absorber, std::less, boost::icl::inplace_plus,boost::icl::inter_section, boost::icl::discrete_interval<int, std::less>, ShmemAllocator> MyMap;
给出以下错误
错误:"模板类Compare、模板类Combine、模板类Section、类Interval、模板类Alloc>类boost::icl::interval_map"的模板形参列表中参数8的类型/值不匹配type pedef boost::icl::interval_map, ShmemAllocator> MyMap;
错误:期望一个类模板,得到' ShmemAllocator{也称为boost::interprocess::allocator, boost::interprocess::segment_manager, boost::interprocess::iset_index>>} '
错误:';'标记前的声明类型无效
目前,编译器错误表明您正在传递类型作为模板参数,而(可变的)模板模板参数是需要的。
从技术上讲,您可以使用c++ 11模板混叠实现这一点¹:
template <typename T> using allocator = bip::allocator<T, smgr>;
template<typename T> using set = std::set<T, allocator<T> >;
template <typename Domain, typename Codomain>
using basic_map = icl::interval_map<Domain, Codomain,
icl::partial_absorber, std::less, icl::inplace_plus, icl::inter_section, icl::discrete_interval<int, std::less>,
allocator
>;
然而,它不会像我在我的实时编码会话中发现的那样导致工作代码。问题是Boost ICL目前显然不支持有状态分配器。
这意味着不是所有的构造函数都使用分配器实例来传递所需的状态。
遗憾的是,这意味着只有一个粗糙的自定义分配器可以工作,其中自定义分配器将通过全局引用引用您的共享内存段。
演示这里有一个演示,展示了这样一个全局段分配器的概念验证:
Live On Coliru
#include <boost/icl/interval_map.hpp>
#include <boost/interprocess/managed_mapped_file.hpp>
#include <iostream>
#include <vector>
#include <set>
namespace bip = boost::interprocess;
namespace icl = boost::icl;
namespace shared {
using segment = bip::managed_mapped_file;
using smgr = segment::segment_manager;
}
namespace {
static bip::managed_mapped_file global_mm(bip::open_or_create, "./demo.bin", 1ul<<20);
static bip::allocator<void, shared::smgr> global_alloc(global_mm.get_segment_manager());
template <class T> struct SimpleAllocator : std::allocator<T> { // inheriting the nested typedefs only
typedef T value_type;
SimpleAllocator() : _alloc(global_alloc) {}
template <class U>
SimpleAllocator(const SimpleAllocator<U> &other) : _alloc(other._alloc) {}
T* allocate(std::size_t n) { return std::addressof(*_alloc.allocate(n)); }
void deallocate(T *p, std::size_t n) { _alloc.deallocate(p, n); }
// optionals
template <typename Other> struct rebind { typedef SimpleAllocator<Other> other; };
bip::allocator<T, shared::smgr> _alloc;
};
template <class T, class U> bool operator==(const SimpleAllocator<T> &, const SimpleAllocator<U> &) { return true; }
template <class T, class U> bool operator!=(const SimpleAllocator<T> &, const SimpleAllocator<U> &) { return false; }
}
namespace shared {
template <typename T> using allocator = SimpleAllocator<T>;
template<typename T> using set = std::set<T, std::less<T>, allocator<T> >;
template <typename Domain, typename Codomain>
using basic_map = icl::interval_map<Domain, Codomain,
icl::partial_absorber, std::less, icl::inplace_plus, icl::inter_section, icl::discrete_interval<int, std::less>,
allocator
>;
using map = basic_map<int, set<int> >;
using interval = map::interval_type;
}
#include <iostream>
int main() {
shared::map demo;
for (auto&& element : {
shared::map::value_type { shared::interval::right_open(4, 5), { 1, 7, } },
shared::map::value_type { shared::interval::right_open(2, 6), { 1, 2, 3, } },
})
{
demo.add(element);
std::cout << "adding: " << element.first << ", result: " << demo << "n";
}
}
我使用managed_mapped_file
而不是maneged_shared_memory
,因为后者不支持在线编译器。
¹或者在c++03中使用"type函数"
- 理解boost::asio-async_read在无需读取内容时的行为
- boost::进程间消息队列引发错误
- 如何运行位于boost/libs/python/example/tutorial目录中的hello.cpp和Jamfil
- cmake如何在fedora工作站中找到boost静态库包
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- Boost Graph Library,修复节点大小
- 什么是"#include <boost/functional/hash.hpp> "?
- 基于boost的程序的静态链接——zlib问题
- boost::icl::interval 和 boost::numeric::interval 是否有可用的适配层?
- 如何比较2个boost :: iCl :: Interval_set的平等
- boost::icl::interval_map重叠并集
- 基于区间的数据结构(类似于boost icl)
- Boost ICL未按预期工作,std::set operator+=应为
- 有没有一种方法可以获得boost::icl::interval_map中的间隔数
- 在为请求选择服务的情况下,使用Boost ICL而不是for_each是否可能且合理
- 如何在使用C++Boost间隔容器库(ICL)时移动间隔
- 使用Boost ICL查找范围包含点的所有对象
- Boost ICL和Boost序列化的组合
- 共享内存中的c++ boost icl容器
- Boost::icl::no_type error