简体   繁体   English

与 boost::interprocess 共享地图

[英]Shared map with boost::interprocess

I have a simple requirement that might be tough to solve.我有一个可能很难解决的简单需求。 I did find some leads like this or this but I can't seem to readilly use them.我确实找到了一些像这样这样的线索,但我似乎无法轻易使用它们。 The former doesn't even translate into buildable code for me.前者甚至没有转化为我的可构建代码。 I am not experienced with Boost to just write this on my own but it seems to me this might be a common requirement.我对 Boost 没有经验,只能自己写这个,但在我看来这可能是一个常见的要求。

I have also come across Interprocess STL Map but I have not yet been able to assemble it into working code.我也遇到过Interprocess STL Map,但我还没有能够将它组装成工作代码。

I am thinking boost::interprocess is the way to go here, unless I want to create some shared memory map from scratch.我认为boost::interprocess是这里的方法,除非我想从头开始创建一些共享内存映射。

I am not concerned with portability.我不关心便携性。 I need a solution that will work with MS compiler, specifically the one that comes with VS 2010.我需要一个可以与 MS 编译器一起使用的解决方案,特别是 VS 2010 附带的解决方案。

This poster seems to want to more or less what I am trying to do, except I need to map a GUID to an arbitrary length binary buffer (but an int to string is equally good as a starting point). 这张海报似乎或多或少想要我想要做的事情,除了我需要将 GUID 映射到任意长度的二进制缓冲区(但 int 到 string 作为起点同样好)。 Unfortunately, I cannot compile the code cleanly to even begin with experiments.不幸的是,我无法干净地编译代码,甚至无法从实验开始。

Also I have two concerns: A) is it possible to automatically (or at least predictably) grow/shrink the shared memory to accommodate allocation needs and B) assuming one process creates the map, how can another process "attach" to it?另外我有两个问题:A)是否可以自动(或至少可预测地)增长/缩小共享内存以适应分配需求和 B)假设一个进程创建了映射,另一个进程如何“附加”到它?

I don't mind if a solution requires multiple shared "segments" in order to satisfy allocation needs.我不介意一个解决方案是否需要多个共享的“段”来满足分配需求。 It doesn't necessarily have to be a single monolithic shared chunk of memory.它不一定是一个单一的共享内存块。

Any help is highly appreciated.任何帮助都受到高度赞赏。

Here's recent example which i had written to learn the usage of maps in Shared memory.这是我最近编写的示例,用于学习共享内存中映射的用法。 It compiles so probably, you can experiment with it to suit your requirement.它很可能编译,您可以试验它以满足您的要求。

The code for a server process which creates Shared memory and puts map into it:-创建共享内存并将映射放入其中的服务器进程的代码:-

#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());

    // 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;
}

The code for a client process which attaches to the memory and accesses map's data.附加到内存并访问地图数据的客户端进程的代码。

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

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;
}
                                                                                                               

The Code for the Client Process explains how using "names" and an offset pointer, other processes can attach and access the Map contents created in SHM by the server process.客户端进程的代码解释了如何使用“名称”和偏移指针,其他进程可以附加和访问服务器进程在 SHM 中创建的 Map 内容。 But, allocating size (here its '65536') while creating a new shared memory segment, i am not sure whether the size can be shrinked, though probably you can create more chunks of shared memory for expanding the SHM...但是,在创建新的共享内存段时分配大小(这里是“65536”),我不确定是否可以缩小大小,尽管您可能可以创建更多的共享内存块来扩展 SHM ......

Hope it helped...希望它有帮助...

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

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