提升icl共享内存访问

时间:2015-08-19 13:47:18

标签: c++ boost boost-icl

我在共享内存中创建了icl map,如下面链接

所述

c++ boost icl containers in shared memory

下面是代码

#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] - &gt; {1 7})} 加入:[2,6],结果:{([2,4] - > {1 2 3})([4,5) - > {1 2 3 7})([5,6] - &gt; ; {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&lt;&lt;人口统计学&GT;尺寸()。我的操作系统是Redhat&amp; boost版本是1.54,C ++ 11。

0 个答案:

没有答案