共享内存中的c++ boost icl容器

c++ boost icl containers in shared memory

本文关键字:boost icl 容器 c++ 内存 共享      更新时间:2023-10-16

我与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函数"