2013-03-10 3 views
0

Boost Interprocess 라이브러리를 사용하여 공유 메모리에 unordered_map을 생성하려고합니다.boost interprocess unordered_map string

#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(std::string("H")).print(); 
: 나는지도에 액세스하려고 그러나, 내가 그러나

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

같은 구문을 사용합니다, 나는 컴파일 오류가 발생 성병 : : 문자열,이 작업을 수행하는 것을 선호

위의 문장을 작성할 수 있습니까? 예 : 공유 메모리에 할당 된 맵에 std :: string으로 액세스합니까?

+0

부스터 사용자 메일 링리스트에 게시했습니다. 그리고 Boost.Interprocess 작성자로부터 답변을 얻지 못했습니다. – Lazylabs

답변

2

나는 부스트 사용자 메일 링리스트에 this를 게시했습니다. 그리고 Boost.Interprocess 작성자로부터 답변을 얻지 못했습니다.

+0

질문에 대한 링크를 추가하십시오. – Ammar

+1

가 지금 링크를 추가했습니다. – Lazylabs