c++ - boost icl shared memory access -
i have created icl map in shared memory described in below link
c++ boost icl containers in shared memory
below code
#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 nested typedefs 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")();; (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"; } }
output : adding: [4,5), result: {([4,5)->{1 7 })} adding: [2,6), result: {([2,4)->{1 2 3 })([4,5)->{1 2 3 7 })([5,6)->{1 2 3 })}
next created program access map
below code
#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 nested typedefs 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 = demo->find(4); if (it != demo->end()) { std::cout << "key :: " << it->first << std::endl; } }
but program crashes @ line std::cout << demo->size(). os redhat & boost version 1.54, c++11.
Comments
Post a Comment