C++ boost icl共享内存访问
我已经在共享内存中创建了icl映射,如下链接所述 下面是代码C++ boost icl共享内存访问,c++,boost,boost-icl,C++,Boost,Boost Icl,我已经在共享内存中创建了icl映射,如下链接所述 下面是代码 #include <boost/icl/interval_map.hpp> #include <boost/interprocess/managed_mapped_file.hpp> #include <boost/interprocess/managed_shared_memory.hpp> #include <iostream> #include <vector> #
#include <boost/icl/interval_map.hpp>
#include <boost/interprocess/managed_mapped_file.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <iostream>
#include <vector>
#include <set>
namespace bip = boost::interprocess;
namespace icl = boost::icl;
namespace shared {
using segment = bip::managed_shared_memory;
using smgr = segment::segment_manager;
}
namespace {
static bip::managed_shared_memory global_mm(boost::interprocess::open_or_create,
"MySharedMemory",65536 //segment name
);
static bip::allocator<void, shared::smgr> global_alloc(global_mm.get_segment_manager());
/*
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 = global_mm.find_or_construct<shared::map>("Store")();;
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";
}
}
#包括
#包括
#包括
#包括
#包括
#包括
名称空间bip=boost::进程间;
名称空间icl=boost::icl;
命名空间共享{
使用段=bip::托管共享内存;
使用smgr=segment::segment\u管理器;
}
名称空间{
静态bip::托管共享内存全局内存(boost::进程间::打开或创建,
“MySharedMemory”,65536//段名
);
静态bip::分配器全局分配(global_mm.get_segment_manager());
/*
静态bip::托管映射文件全局(bip::打开或创建“./demo.bin”,1ulend()){
std::好吧,在我“存储”的地方有一个痛苦的错误SimpleAllocator::_alloc
中的全局alloc显然无法工作,因为分配器将是共享内存中数据结构的一部分,因此段管理器地址将存储在那里。哎呀。但是我也无法通过直接调用global\u alloc
使其工作。请参阅此处了解我的WIP:我想boost interval容器仅支持std::allocators!唯一的解决方法是使此分配器指向共享内存段并相应地分配对象
#include <boost/icl/interval_map.hpp>
#include <boost/interprocess/managed_mapped_file.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <iostream>
#include <vector>
#include <set>
namespace bip = boost::interprocess;
namespace icl = boost::icl;
namespace shared {
using segment = bip::managed_shared_memory;
using smgr = segment::segment_manager;
}
namespace {
static bip::managed_shared_memory global_mm(boost::interprocess::open_only,
"MySharedMemory");
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 = global_mm.find_or_construct<shared::map>("Store")();
std::cout << demo->size();
auto it = demo->find(4);
if (it != demo->end()) {
std::cout << "key :: " << it->first << std::endl;
}
}