首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >与boost::interprocess共享映射

与boost::interprocess共享映射
EN

Stack Overflow用户
提问于 2012-09-14 03:02:01
回答 1查看 10.5K关注 0票数 6

我有一个简单的需求,可能很难解决。我确实找到了一些线索,比如thisthis,但我似乎不能很好地使用它们。前者甚至不能为我翻译成可构建的代码。我没有使用Boost的经验,只能自己写这篇文章,但对我来说,这似乎是一种常见的需求。

我也遇到过Interprocess STL Map,但我还不能将它组装成可工作的代码。

我认为boost::interprocess是最好的选择,除非我想从头开始创建一些共享内存映射。

我不关心可移植性。我需要一个解决方案,将与微软编译器,特别是与VS2010。

This poster似乎或多或少想要做我想要做的事情,除了我需要将GUID映射到任意长度的二进制缓冲区(但int到字符串作为起点也是一样好的)。不幸的是,我甚至不能从实验开始就干净利落地编译代码。

我还有两个顾虑: A)是否有可能自动(或至少可预测地)增加/缩小共享内存以适应分配需求;B)假设一个进程创建了映射,另一个进程如何“附加”到它?

我不介意一个解决方案需要多个共享的“段”来满足分配需求。它不一定非得是单一的共享内存块。

任何帮助都是非常感谢的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-10-25 02:51:03

这是我最近编写的一个示例,目的是了解如何在共享内存中使用映射。它很可能会编译,你可以根据你的需求进行实验。

创建共享内存并将map放入其中的服务器进程的代码:

代码语言:javascript
复制
#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;
}

客户端进程的代码,它连接到内存并访问map的数据。

代码语言:javascript
复制
#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;
}

客户端进程的代码解释了如何使用“名称”和偏移量指针,其他进程可以附加和访问由服务器进程在SHM中创建的Map内容。但是,在创建新的共享内存段时分配大小(这里是'65536'),我不确定大小是否可以缩小,尽管您可能可以创建更多的共享内存块来扩展SHM...

希望能帮上忙。

票数 18
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12413034

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档