【问题标题】:Boost template error using inter-process map使用进程间映射提升模板错误
【发布时间】:2015-05-28 17:57:54
【问题描述】:

编辑:虽然严格来说不是我正在寻找的答案,但那是因为我问错了问题。我已将此问题标记为“已回答”,因为它帮助我了解自己做错了什么(回到上下文中,类结构没有意义,需要使用类似的更改以不同方式重做),我认为海报值得修复他所说的“非标准”的代表提升。


请花时间帮助解决这个问题。如果你对 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); }

我无计可施。请帮忙!

【问题讨论】:

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


    【解决方案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 进程间tutorial

    【讨论】:

    • 我确实按照教程进行了操作。这有点奇怪,因为 (a) 它从其他代码中删除,我想让它完整供人们阅读并能够看到相同的错误,以及 (b) 因为实际上有两个 IPC 池。有一个具有“控制类”的 IPC 池和另一个由它“控制”的 IPC 池。有一些原因与传统内存模型有关。
    • 当回到我的类的上下文中时,这让我意识到类结构是错误的(实际上没有意义)。感谢那。我知道如何正确地做事。正如编辑中解释的那样,出于给出的原因,我已将此标记为已回答。
    猜你喜欢
    • 1970-01-01
    • 2012-01-20
    • 2022-01-23
    • 1970-01-01
    • 2021-03-03
    • 2011-06-12
    • 1970-01-01
    • 2020-01-01
    • 2023-01-19
    相关资源
    最近更新 更多