简体   繁体   English

使用int和string来提升进程间映射

[英]boost interprocess map with int and string

I have the following code that saves a map into shared memory using boost interprocess 我有以下代码使用boost进程将映射保存到共享内存中

    using namespace boost::interprocess;
//Shared memory front-end that is able to construct objects
//associated with a c-string. Erase previous shared memory with the name
//to be used and create the memory segment at the specified address and initialize resources
shared_memory_object::remove("MySharedMemory");

try{
    managed_shared_memory segment
        (create_only
         ,"MySharedMemory" //segment name
         ,655360);          //segment size in bytes

    //Note that map<Key, MappedType>'s value_type is std::pair<const Key, MappedType>,
    //so the allocator must allocate that pair.
    typedef allocator<char, managed_shared_memory::segment_manager> CharAllocator;
    typedef basic_string<char, std::char_traits<char> ,CharAllocator> MyShmString;
    typedef allocator<MyShmString, managed_shared_memory::segment_manager> StringAllocator;

    typedef int    KeyType;
    typedef std::pair<const int, StringAllocator> ValueType;
    typedef StringAllocator MappedType;

    typedef allocator<ValueType, managed_shared_memory::segment_manager> ShmemAllocator;

    typedef map<KeyType, MappedType, std::less<KeyType>, ShmemAllocator> MyMap;

    //Initialize the shared memory STL-compatible allocator
    ShmemAllocator alloc_inst (segment.get_segment_manager());
    CharAllocator charallocator  (segment.get_segment_manager());


    //Construct a shared memory map.
    //Note that the first parameter is the comparison function,
    //and the second one the allocator.
    //This the same signature as std::map's constructor taking an allocator
    MyMap *mymap =
        segment.construct<MyMap>("MyMap")      //object name
        (std::less<int>() //first  ctor parameter
         ,alloc_inst);     //second ctor parameter

    //Insert data in the map
    MyShmString mystring(charallocator);
    mystring = "this is my text";
    for(int i = 0; i < 100; ++i){
        //mymap[i] = mystring;
        mymap->insert(std::pair<const int, MappedType>(i, mystring));
    }
}

this code doesnt compile.. it throws the following error 这段代码没有编译..它抛出以下错误

no matching function for call to ‘std::pair<const int, boost::interprocess::allocator<boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> > >, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> > >::pair(int&, main()::MyShmString&)’
    /usr/include/c++/4.2.1/bits/stl_pair.h:84: note: candidates are: std::pair<_T1, _T2>::pair(const _T1&, const _T2&) [with _T1 = const int, _T2 = boost::interprocess::allocator<boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> > >, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> >]
    /usr/include/c++/4.2.1/bits/stl_pair.h:80: note:                 std::pair<_T1, _T2>::pair() [with _T1 = const int, _T2 = boost::interprocess::allocator<boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> > >, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> >]
    /usr/include/c++/4.2.1/bits/stl_pair.h:69: note:                 std::pair<const int, boost::interprocess::allocator<boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> > >, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> > >::pair(const std::pair<const int, boost::interprocess::allocator<boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> > >, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> > >&)

the difference if 差异如果

::pair(int&, main()::MyShmString&)

so i am guessing 所以我猜

            mymap->insert(std::pair<const int, MappedType>(i, mystring));

is not the correct way to go.. so how should i insert into the map, if the error is there.. else what is the error? 不是正确的方法..所以我应该如何插入到地图中,如果错误在那里..否则是什么错误?

Surely the value_type is: 肯定value_type是:

typedef std::pair<const int, MyShmString > ValueType;

not

typedef std::pair<const int, StringAllocator> ValueType;

And similarly: 同样地:

typedef MyShmString MappedType;

not

typedef StringAllocator MappedType;

Your types are different, is that intentional? 你的类型不同,是故意的吗? MyShmString is not the same as MappedType - may be you ought to change the following line: MyShmStringMappedType - 可能您应该更改以下行:

typedef StringAllocator MappedType;

to

typedef MyShmString MappedType;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM