使用进程间映射增强模板错误

时间:2015-05-28 17:57:55

标签: c++ templates boost interprocess boost-interprocess

编辑:虽然不是我所寻找的答案,但这是因为我提出了错误的问题。我已将此问题标记为"已回答"因为它帮助我看到我做错了什么(当回到上下文中时,类结构没有意义,并且需要使用类似的更改进行不同的重做)并且我认为海报应该得到代表性的提升来修复,正如他所说的,"非标准。"

请花时间帮助解决这个问题。如果你对Boost的经验比我更有经验,那就不难了。

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

可以在http://pastebin.com/caNNdztt找到以语法突出显示的代码(虽然我忘了在代码中没有出现#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); }

从该标题中的上述语句中替换它:

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

我的智慧结束了。请帮忙!

1 个答案:

答案 0 :(得分:1)

您需要致电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);
}

你是以一种“非标准”方式进行的,所以我不知道该代码是否有效。为什么不关注boost interprocess tutorial