-->

共享地图的boost ::间(Shared map with boost::interprocess

2019-09-22 10:21发布

我有一个简单的要求,可能是很难解决的。 我也找到了一些线索像这样或这样 ,但我似乎无法readilly使用它们。 前者甚至没有转化为可建我的代码。 我不与升压经历只写这对我自己的,但在我看来,这可能是一个普遍的要求。

我也遇到进程间STL地图 ,但我现在还没有能够将它组装成工作代码。

我想到boost::interprocess是走在这里,除非我想从头开始创建一些共享内存映射的方式。

我不关心可移植性。 我需要一个解决方案,将与MS的编译器,特别是随VS 2010的工作之一。

这张海报似乎想或多或少什么,我试图做的,但我需要一个GUID映射到一个任意长度的二元缓冲(但一个int字符串是为出发点一样的好)。 不幸的是,我不能编译的代码干净,甚至与实验开始。

另外我有两个问题:A)是它可以自动地(或至少可预测)生长/收缩共享存储器以适应分配需求和B)假设一个过程创建地图,如何能另一过程“附加”到它?

如果解决方案需要多个共享“段”,以满足需求分配,我不介意。 这并不一定必须是存储器的单个单片共享块。

任何帮助,高度赞赏。

Answer 1:

下面是我曾写信给学习共享内存映射的使用最近的例子。 它编译所以可能的是,你可以用它进行试验,以满足您的要求。

为它创建的共享存储器,并把映射到它的服务器处理的代码: -

#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/containers/map.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <functional>
#include <utility>

int main ()
{
    using namespace boost::interprocess;

    // remove earlier existing SHM
    shared_memory_object::remove("SharedMemoryName");

    // create new 
    managed_shared_memory segment(create_only,"SharedMemoryName",65536);

    //Note that map<Key, MappedType>'s value_type is std::pair<const Key, MappedType>,
    //so the allocator must allocate that pair.
    typedef int    KeyType;
    typedef float  MappedType;
    typedef std::pair<const int, float> ValueType;

    //allocator of for the map.
    typedef allocator<ValueType, managed_shared_memory::segment_manager> ShmemAllocator;

    //third parameter argument is the ordering function is used to compare the keys.
    typedef map<KeyType, MappedType, std::less<KeyType>, ShmemAllocator> MySHMMap;

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

    //Construct a shared memory map.
    MySHMMap *mymap =  segment.construct<MySHMMap>("MySHMMapName") (std::less<int>() ,alloc_inst);

    // offset ptr within SHM for map 
    offset_ptr<MySHMMap> m_pmap = segment.construct<MySHMMap>("MySHMMapName")(std::less<int>(), alloc_inst);

    //Insert data in the map
    for(int i = 0; i < 10; ++i)
    {
            m_pmap->insert(std::pair<const int, float>(i, (float)i));
    }

    return 0;
}

为此附着到存储器并访问地图的数据的客户端进程的代码。

#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/containers/map.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <functional>
#include <utility>

int main ()
{
    using namespace boost::interprocess;

    try
    {

            managed_shared_memory segment(open_or_create, "SharedMemoryName",65536);

            //Again the map<Key, MappedType>'s value_type is std::pair<const Key, MappedType>, so the allocator must allocate that pair.
            typedef int    KeyType;
            typedef float  MappedType;
            typedef std::pair<const int, float> ValueType;

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

            //The map
            typedef map<KeyType, MappedType, std::less<KeyType>, ShmemAllocator> MySHMMap;

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

            //access the map in SHM through the offset ptr                                                         
            MySHMMap :: iterator iter;
            offset_ptr<MySHMMap> m_pmap = segment.find<MySHMMap>("MySHMMapNAme").first;

            iter=m_pmap->begin();
            for(; iter!=m_pmap->end();iter++)
            {
                   std::cout<<"\n "<<iter->first<<" "<<iter->second;
            }
    }catch(std::exception &e)            
    {
            std::cout<<" error  " << e.what() <<std::endl;
            shared_memory_object::remove("SharedMemoryName");
    }
    return 0;
}

守则客户端进程说明如何使用“名”和偏移指针,其他进程可以连接并通过服务器访问的过程中SHM创建的地图内容。 但是,分配的大小(这里的“65536”),并创建一个新的共享内存段,我不知道尺寸是否可以收缩,虽然可能可以用于扩展SHM创建共享内存的多块......

希望它帮助...



文章来源: Shared map with boost::interprocess