如何返回可以转换为存储在boost :: interprocess映射中的值元素的不透明句柄(void*或dword)?

War*_* P 7 c++ boost boost-interprocess visual-c++-2008 visual-c++

我对将std::string密钥和大struct值放入容器之类所涉及的堆和按值与引用语义有点混淆boost::interprocess::map.

这是我的情况,以及我正在使用的一些typedef:

typedef std::string     AreaKeyType;     
typedef DATA_AREA_DESC          AreaMappedType; // DATA_AREA_DESC is a big struct.
typedef std::pair<const AreaKeyType, AreaMappedType> AreaValueType;
typedef boost::interprocess::allocator<AreaValueType, boost::interprocess::managed_shared_memory::segment_manager> AreaShmemAllocator;
typedef boost::interprocess::map<AreaKeyType, AreaMappedType, std::less<AreaKeyType>, AreaShmemAllocator> AreaMap;
Run Code Online (Sandbox Code Playgroud)

这是我如何插入AreaValueType(这是std :: pair的typedef):

 AreaValueType A(areaKey, arearec);
 anAreaMap->insert(A); 
Run Code Online (Sandbox Code Playgroud)

我相信上面的代码将我在本地(非共享内存)堆栈上的std :: pair复制到共享内存区域.我可以在boost :: interprocess :: map中获取该共享内存区域的句柄,还是仅限于将该记录整回并整个存储?(换句话说,我可以将类似结构的东西存储到boost进程间映射中,然后更新该记录中的单个字节,或者我必须通过替换DATA_AREA_DESC结构中的所有字节来更新整个记录,全新字节).

进一步澄清:

  1. 我有一个普通的旧ANSI C DLL导出api,内部使用C++和Boost :: interprocess :: map.该函数应该在地图中创建一个项目,然后返回一个句柄.如何在boost :: interprocess :: map中插入一些东西,然后将一个句柄返回给非C++用户,最好是强制转换为void*或者unsigned long?我所能做的就是通过查找std :: string键值从共享内存中获取内容,并将新记录写入内存.我希望能够保持对共享内存对象的引用.

  2. 如果我不能直接这样做,我将如何间接地做到这一点?我想我可以保持一个非共享内存的std ::向量,并分配一个非共享内存的std :: string持有areaKey,这是一个std :: string的值,然后做的投void*项目返回std::string然后使用它从共享内存区域中获取记录.对于一些如此基本的东西来说,这似乎都是非常必要的工作.也许boost :: interprocess :: map不是我的要求的正确选择?

我试过了什么?这个,编译,但我不知道我是否正确这样做.不知何故,我觉得丑陋内部取消引用::iterator返回find,然后立即采取其地址如下:

void ** handle; // actually a parameter in my api.
*handle = (void*)&(*anAreaMap->find(areaKey));
Run Code Online (Sandbox Code Playgroud)

更新以上工作.然而,下面答案中非常明智的建议不起作用.使用boost :: interprocess :: string会导致完整和完全失败并在运行时崩溃.使用std :: string,除非特别是Boost编码std :: string支持的作者,否则无权工作,实际上效果很好.

rha*_*oto 1

如果handle应该是指向共享内存中的指针,那么只要您知道它在映射中,std::pair您的代码就可以工作。它没有任何问题,只是您不需要显式强制转换(如果您确实强制转换,那么将是首选)。areaKeystatic_cast<void*>()

我没有使用过boost::interprocess,但我认为您需要使用boost::interprocess::stringstd::basic_string带有非默认分配器的密钥。boost::interprocess除非在幕后做了一些奇特的事情,否则 using会将std::string指向本地内存(用于字符串缓冲区)的指针放入共享内存中,这在另一个进程中没有意义。

这是一个使用带有字符串键的映射的测试程序:

#include <iostream>
#include <string>
#include <boost/foreach.hpp>
#include <boost/format.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/containers/map.hpp>
#include <boost/interprocess/containers/string.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>

namespace bi = boost::interprocess;

#define SHARED_STRING 1 // set to 1 for interprocess::string, 0 for std::string
static const char *SHARED_MEMORY_NAME = "MySharedMemory";
static const char *SHARED_MAP_NAME = "MySharedMap";

int main(int argc, char *argv[]) {
#if SHARED_STRING
   typedef bi::allocator<char, bi::managed_shared_memory::segment_manager> CharAllocator;
   typedef bi::basic_string<char, std::char_traits<char>, CharAllocator> Key;
#else
   typedef std::allocator<char> CharAllocator;
   typedef std::basic_string<char, std::char_traits<char>, CharAllocator> Key;
#endif

   typedef int Mapped;
   typedef std::pair<const Key, Mapped> Value;
   typedef bi::allocator<Value, bi::managed_shared_memory::segment_manager> MapAllocator;
   typedef bi::map<Key, Mapped, std::less<Key>, MapAllocator> Map;

   bi::managed_shared_memory *segment;
   Map *map;
   if (argc <= 1) {
      // Create new shared memory segment.
      bi::shared_memory_object::remove(SHARED_MEMORY_NAME);
      segment = new bi::managed_shared_memory(bi::create_only, SHARED_MEMORY_NAME, 65536);

      MapAllocator mapAllocator(segment->get_segment_manager());
      map = segment->construct<Map>(SHARED_MAP_NAME)(std::less<Key>(), mapAllocator);
      assert(map);
   }
   else {
      // Open existing shared memory segment.
      segment = new bi::managed_shared_memory(bi::open_only, SHARED_MEMORY_NAME);

      map = segment->find<Map>(SHARED_MAP_NAME).first;
      assert(map);
   }

#if SHARED_STRING
   CharAllocator charAllocator(segment->get_segment_manager());
#else
   CharAllocator charAllocator;
#endif
   while (true) {
      std::string input;
      if (!getline(std::cin, input))
         break;

      map->insert(std::make_pair(Key(input.begin(), input.end(), charAllocator), 0));

      BOOST_FOREACH(const Value& value, *map)
         std::cout << boost::format("('%s',%d)\n") % value.first % value.second;
   }

   delete segment;
   bi::shared_memory_object::remove(SHARED_MEMORY_NAME);

   return 0;
}
Run Code Online (Sandbox Code Playgroud)

不带任何参数运行它以创建一个新的共享内存段,并至少使用一个参数来打开现有的共享内存段(无参数调用必须已在运行)。在这两种情况下,程序都会迭代地从 读取键stdin,将条目插入到映射中,并将内容写入到stdout