共享地图与boost :: interprocess [英] Shared map with boost::interprocess

查看:226
本文介绍了共享地图与boost :: interprocess的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的要求可能难以解决。我确实找到了一些线索,例如这个,但我似乎无法使用它们。前者甚至不能为我翻译成可构建的代码。我没有经验的Boost只是自己写这个,但在我看来,这可能是一个常见的要求。



我也遇到了进程间STL映射,但我还没有能够将其组装成工作代码。 / p>

我正在考虑 boost :: interprocess 是去这里的方式,除非我要创建一些共享内存映射从零开始。



我不关心可移植性。我需要一个可与MS编译器配合使用的解决方案,特别是VS 2010附带的解决方案。



这个海报似乎想要或多或少地想要做,除了我需要将GUID映射到任意长度的二进制缓冲区(但是int的字符串作为起点是同等的)。不幸的是,我无法编辑代码,甚至开始实验。



另外我有两个问题:A)是否可以自动(或至少可预测)增长/缩小共享内存以适应分配需求,并且B)假设一个进程创建地图,另一个进程如何附加?



我不介意如果解决方案需要多个共享的段才能满足分配需求。它不一定是一个单一的单独的共享内存块。



任何帮助都非常感谢。

解决方案

这是我最近一次为共享内存学习地图使用情况的例子。它可能会进行编译,您可以根据需要进行实验。



创建共享内存并将其映射到其中的服务器进程的代码: -

  #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()
{
使用命名空间boost :: interprocess;

//删除早期的现有SHM
shared_memory_object :: remove(SharedMemoryName);

//创建新
managed_shared_memory段(create_only,SharedMemoryName,65536);

//请注意,映射< Key,MappedType>的value_type是std :: pair< const Key,MappedType>
//所以分配器必须分配该对。
typedef int KeyType;
typedef float MappedType;
typedef std :: pair< const int,float>值类型;

//分配给地图。
typedef allocator< ValueType,managed_shared_memory :: segment_manager> ShmemAllocator;

//第三个参数参数是用于比较键的排序函数。
typedef map< KeyType,MappedType,std :: less< KeyType>,ShmemAllocator> MySHMMap;

//初始化共享内存STL兼容分配器
ShmemAllocator alloc_inst(segment.get_segment_manager());

//构建共享内存映射。
MySHMMap * mymap = segment.construct< MySHMMap>(MySHMMapName)(std :: less< int>(),alloc_inst);

// SHM内的offset ptr对于map
offset_ptr< MySHMMap> m_pmap = segment.construct< MySHMMap>(MySHMMapName)(std :: less< int>(),alloc_inst);

//在地图中插入数据
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()
{
使用命名空间boost :: interprocess;

try
{

managed_shared_memory segment(open_or_create,SharedMemoryName,65536);

//再次映射< Key,MappedType>的value_type是std :: pair< const Key,MappedType>所以分配器必须分配该对。
typedef int KeyType;
typedef float MappedType;
typedef std :: pair< const int,float>值类型;

//分配分配器
typedef分配器< ValueType,managed_shared_memory :: segment_manager> ShmemAllocator;

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

//初始化共享内存STL兼容分配器
ShmemAllocator alloc_inst(segment.get_segment_manager());

//通过偏移量访问SHM中的映射ptr
MySHMMap :: iterator iter;
offset_ptr< MySHMMap> m_pmap = segment.find< MySHMMap>(MySHMMapNAme)。

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

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



希望帮助...


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.

I have also come across Interprocess STL Map but I have not yet been able to assemble it into working code.

I am thinking boost::interprocess is the way to go here, unless I want to create some shared memory map from scratch.

I am not concerned with portability. I need a solution that will work with MS compiler, specifically the one that comes with 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). 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?

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

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

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>

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. 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...

Hope it helped...

这篇关于共享地图与boost :: interprocess的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆