0

Boost Interprocess ライブラリを使用して共有メモリに unordered_map を作成しようとしています。ここに、私が使用しようとしているコードがあります(Boost Interprocessのドキュメントから例を挙げます):

#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <functional>
#include <boost/functional/hash.hpp>
#include <boost/unordered_map.hpp>
#include <iostream>
#include <string>
#include <boost/interprocess/containers/string.hpp>

namespace bipc = boost::interprocess;

typedef bipc::allocator<char, bipc::managed_shared_memory::segment_manager> CharAllocator;
typedef bipc::basic_string<char, std::char_traits<char>, CharAllocator> ShmemString;


struct Person
{
  int age;
  ShmemString name;
  double salary;

  Person(int i,
     double sal,
     const char* s,
     const char_allocator& a)
    : age(i),
      name(s, a),
      salary(sal)
    {
    }
  void print() {}
}

typedef ShmemString KeyType;
typedef Person MappedType;

typedef std::pair< KeyType, MappedType > MapPersonType;


typedef bipc::allocator< MapPersonType,
                     bipc::managed_shared_memory::segment_manager >
ShMemAllocator;

typedef boost::unordered_map< KeyType,
                          MappedType,
                          boost::hash<KeyType>,
                          std::equal_to<KeyType>,
                          ShMemAllocator >
PersonMap;

これは私がメインプログラムでやろうとしていることです:

int main() 
{
bipc::managed_shared_memory segment(bipc::create_only, "MySharedMemory", 65536);

PersonMap *persons = segment.construct<PersonMap>("MyHashMap")
    ( 3, boost::hash<ShmemString>(), std::equal_to<ShmemString>()
        , segment.get_allocator<MapPersonType>());

char_allocator alloc(segment.get_allocator<char>());

Person p1(20, 10000, "ABC", alloc);
persons->insert(MapPersonType(ShmemString("Person1", alloc), p1));
}

上記のコードを使用すると、共有メモリに unordered_map を作成できます。ただし、マップにアクセスしようとすると、次のような構文を使用する必要があります

persons->at(ShmemString("H", segment.get_allocator<char>())).print();

ただし、コンパイル エラーが発生する std::string でこれを行うことをお勧めします。

persons->at(std::string("H")).print();

上記のステートメント、つまり std::string を使用して共有メモリに割り当てられたマップにアクセスすることは可能ですか?

4

1 に答える 1

2

これを boost-users メーリング リストに投稿しました。そして、Boost.Interprocess の作成者から、これはできないという回答がありました。

于 2013-03-15T16:04:52.387 に答える