повысить доступ к общей памяти ICL

Я создал карту ICL в общей памяти, как описано в ссылке ниже

C ++ Boost 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 <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";
}
}

Выход:
добавление: [4,5), результат: {([4,5) -> {1 7})}
добавив: [2,6), результат: {([2,4) -> {1 2 3}) ([4,5) -> {1 2 3 7}) ([5,6) -> {1 2 3})}

Затем я создал другую программу только для доступа к этой карте

Ниже приведен код

#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;

}

}

Но эта программа вылетает в этой строке std :: cout << демо> размер (). Моя ОС Redhat & буст версия 1.54, C ++ 11.

2

Решение

Задача ещё не решена.

Другие решения