使用 inter-process 地图提升模板错误

Boost template error using inter-process map

编辑:虽然严格来说不是我要找的答案,但那是因为我问错了问题。我将这个问题标记为 "answered" 因为它帮助我看到我做错了什么(当回到上下文中时, class 结构没有意义,需要使用类似的更改以不同的方式重做)而且我认为张贴者应该得到代表的提升,因为他修复了他所说的 "non-standard."


请花时间帮助解决这个问题。如果你对 Boost 的经验比我多,那应该不难。

我正在使用 Boost 1.57 进行 IPC。我正在尝试制作 pair(void *, struct) 的共享映射。这是一个完整的程序(短)。您可以将其插入 MSVC(或者,更改 main() 声明并将其更改为常规 managed_shared_memory、GCC 或 Clang)。

代码可以在http://pastebin.com/caNNdztt找到syntax-highlighted(虽然我忘了去掉代码中不存在的#define的一个用法,它被替换为0x30000000)

#include <iostream>
#include <boost/interprocess/mapped_region.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/managed_windows_shared_memory.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/containers/map.hpp>


using namespace boost::interprocess;

typedef void* mem_managed_type; // GLE_GLOBAL is just void *, but changing it to int doesn't work
struct global_ptr_state; // This is just a POD struct, the definition doesn't matter

//typedef managed_shared_memory mem_manager_t;
typedef managed_windows_shared_memory mem_manager_t;
typedef std::pair<const mem_managed_type, global_ptr_state> global_state_pair;
typedef mem_manager_t::segment_manager segm_t;
typedef allocator< global_state_pair, segm_t > my_global_allocator;
typedef std::less<mem_managed_type> mem_managed_type_less;
typedef mem_manager_t::segment_manager segm_manager;
typedef map<const mem_managed_type, global_ptr_state, mem_managed_type_less, my_global_allocator> global_ptr_pair_map;

int _tmain(int argc, _TCHAR* argv[])
{
    segm_manager *_segm_manager;
    my_global_allocator *_allocatr;
    global_ptr_pair_map *global_map;
    mem_manager_t managed_pool;

    managed_pool = mem_manager_t(create_only, "thisdoesntmatter", 65536, (void *)0x3C000000);
    shared_memory_object _stator_share(open_only, "thisdoesntmattereither", read_write);
    mapped_region _stator_region(_stator_share, read_write, 0, 0, (void *)0x30000000);
    _segm_manager = managed_pool.get_segment_manager();
    _allocatr = new my_global_allocator(_segm_manager);

    // This line throws the error
    global_map = new global_ptr_pair_map; // This line causes the compilation error
}

错误的简短版本是

1>c:\boost\include\boost-1_57\boost\container\map.hpp(493): error C2535: 'std::pair<boost::container::container_detail::iterator<boost::intrusive::tree_iterator<boost::intrusive::bhtraits<T,boost::intrusive::rbtree_node_traits<boost::interprocess::offset_ptr<void,ptrdiff_t,size_t,0>,true>,normal_link,boost::intrusive::default_tag,3>,false>,false>,bool> boost::container::map<const mem_managed_type,global_ptr_state,mem_managed_type_less,my_global_allocator,boost::container::tree_assoc_defaults>::insert(const std::pair<const mem_managed_type,global_ptr_state> &)'
: member function already defined or declared

可以在 Pastebin 上找到整个错误

http://pastebin.com/gjk1e1N7

据我所知,它在抱怨这个:

std::pair<iterator,bool> insert(const nonconst_value_type& x)
   { return this->base_t::insert_unique(x); }

将上面的语句替换为 header:

   std::pair<iterator,bool> insert(const value_type& x)
   { return this->base_t::insert_unique(x); }

我已经无计可施了。请帮忙!

您需要调用 construct 来在 shmem 中创建地图而不是使用 new,另外还有一些修复:

typedef void* mem_managed_type; // GLE_GLOBAL is just void *, but changing it to int doesn't work
struct global_ptr_state{}; // This is just a POD struct, the definition doesn't matter

typedef managed_windows_shared_memory mem_manager_t;
typedef std::pair<const mem_managed_type, global_ptr_state> global_state_pair;
typedef mem_manager_t::segment_manager segm_t;
typedef allocator< global_state_pair, segm_t > my_global_allocator;
typedef std::less<mem_managed_type> mem_managed_type_less;
typedef mem_manager_t::segment_manager segm_manager;
typedef map<mem_managed_type, global_ptr_state, mem_managed_type_less, my_global_allocator> global_ptr_pair_map;

int _tmain(int argc, _TCHAR* argv[])
{
    segm_manager *_segm_manager;
    global_ptr_pair_map *global_map;
    mem_manager_t managed_pool;

    managed_pool = mem_manager_t(create_only, "thisdoesntmatter", 65536, (void *)0x3C000000);
    shared_memory_object _stator_share(open_only, "thisdoesntmattereither", read_write);
    mapped_region _stator_region(_stator_share, read_write, 0, 0, (void *)0x30000000);
    _segm_manager = managed_pool.get_segment_manager();
    my_global_allocator _allocatr = _segm_manager;

    global_map = managed_pool.construct<global_ptr_pair_map>("mymap")(mem_managed_type_less(), _allocatr);
}

您的做法有点"non-standard",所以我无法判断该代码是否有效。为什么不遵循 boost interprocess tutorial?