共享内存中使用boost::lockfree::queue出现问题(boost 1.53,gcc 4.7.2 / clang 3.0-6ubuntu3)

9

我有一个问题,需要将boost::lockfree::queue<<T, fixed_sized<false>, ..>放入共享内存中。我需要这样做是因为我需要能够向队列中插入超过65535个消息,而固定大小队列的限制是65535。
下面的代码可以正常工作(但capacity<...>选项意味着fixed_sized<true>):

typedef boost::interprocess::allocator<
    MessageT, 
    boost::interprocess::managed_shared_memory::segment_manager>
        ShmemAllocator;
typedef boost::lockfree::queue<
    MessageT,
    boost::lockfree::capacity<65535>,
    boost::lockfree::allocator<ShmemAllocator> >
        Queue;
m_segment = new boost::interprocess::managed_shared_memory(
    boost::interprocess::create_only, segmentName, size);
Queue* m_queue = m_segment->construct<Queue>(
    queueName)(
    m_segment->get_segment_manager());
...
m_queue->bounded_push(message);

以下代码也能正常工作(但它不使用共享内存):
boost::lockfree::queue<MessageT> q;
....
q.bounded_push(message);

但是当我尝试将它们结合起来时:

typedef boost::interprocess::allocator<
    MessageT, 
    boost::interprocess::managed_shared_memory::segment_manager>
        ShmemAllocator;
typedef boost::lockfree::queue<
    MessageT,
    boost::lockfree::allocator<ShmemAllocator> >
        Queue;
m_segment = new boost::interprocess::managed_shared_memory(
    boost::interprocess::create_only, segmentName, size);
Queue* m_queue = m_segment->construct<Queue>(
    queueName)(
    m_segment->get_segment_manager());
...
m_queue->bounded_push(message);

该代码无法编译,并且出现以下记录:

In file included from src/model/Queue.h:16:

In file included from /home/uppi/lib/include/boost/lockfree/queue.hpp:24:

/home/uppi/lib/include/boost/lockfree/detail/freelist.hpp:171:28: error: no viable conversion from 'pointer' (aka 'offset_ptr<boost::lockfree::queue<PacketMessage,
      boost::lockfree::allocator<boost::interprocess::allocator<PacketMessage, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family,
      boost::interprocess::offset_ptr<void, long, unsigned long, 0>, 0>, iset_index> > >, boost::parameter::void_, boost::parameter::void_>::node, long, unsigned long, 0UL>') to
      'boost::lockfree::queue<PacketMessage, boost::lockfree::allocator<boost::interprocess::allocator<PacketMessage, boost::interprocess::segment_manager<char,
      boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void, long, unsigned long, 0>, 0>, iset_index> > >, boost::parameter::void_,
      boost::parameter::void_>::node *'
                    return Alloc::allocate(1);
                           ~~~~~~~~~~~~~~~~~

/home/uppi/lib/include/boost/lockfree/detail/freelist.hpp:157:20: note: in instantiation of function template specialization 'boost::lockfree::detail::freelist_stack<boost::lockfree::queue<PacketMessage,
      boost::lockfree::allocator<boost::interprocess::allocator<PacketMessage, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family,
      boost::interprocess::offset_ptr<void, long, unsigned long, 0>, 0>, iset_index> > >, boost::parameter::void_, boost::parameter::void_>::node,
      boost::interprocess::allocator<boost::lockfree::queue<PacketMessage, boost::lockfree::allocator<boost::interprocess::allocator<PacketMessage, boost::interprocess::segment_manager<char,
      boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void, long, unsigned long, 0>, 0>, iset_index> > >, boost::parameter::void_,
      boost::parameter::void_>::node, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void, long, unsigned
      long, 0>, 0>, iset_index> > >::allocate_impl<true>' requested here
            return allocate_impl<Bounded>();


/home/uppi/lib/include/boost/lockfree/detail/freelist.hpp:89:20: note: in instantiation of function template specialization 'boost::lockfree::detail::freelist_stack<boost::lockfree::queue<PacketMessage,
      boost::lockfree::allocator<boost::interprocess::allocator<PacketMessage, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family,
      boost::interprocess::offset_ptr<void, long, unsigned long, 0>, 0>, iset_index> > >, boost::parameter::void_, boost::parameter::void_>::node,
      boost::interprocess::allocator<boost::lockfree::queue<PacketMessage, boost::lockfree::allocator<boost::interprocess::allocator<PacketMessage, boost::interprocess::segment_manager<char,
      boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void, long, unsigned long, 0>, 0>, iset_index> > >, boost::parameter::void_,
      boost::parameter::void_>::node, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void, long, unsigned
      long, 0>, 0>, iset_index> > >::allocate<true, true>' requested here
        T * node = allocate<ThreadSafe, Bounded>();


/home/uppi/lib/include/boost/lockfree/queue.hpp:281:34: note: in instantiation of function template specialization 'boost::lockfree::detail::freelist_stack<boost::lockfree::queue<PacketMessage,
      boost::lockfree::allocator<boost::interprocess::allocator<PacketMessage, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family,
      boost::interprocess::offset_ptr<void, long, unsigned long, 0>, 0>, iset_index> > >, boost::parameter::void_, boost::parameter::void_>::node,
      boost::interprocess::allocator<boost::lockfree::queue<PacketMessage, boost::lockfree::allocator<boost::interprocess::allocator<PacketMessage, boost::interprocess::segment_manager<char,
      boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void, long, unsigned long, 0>, 0>, iset_index> > >, boost::parameter::void_,
      boost::parameter::void_>::node, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void, long, unsigned
      long, 0>, 0>, iset_index> > >::construct<true, true, PacketMessage, boost::lockfree::queue<PacketMessage, boost::lockfree::allocator<boost::interprocess::allocator<PacketMessage,
      boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void, long, unsigned long, 0>, 0>, iset_index> > >,
      boost::parameter::void_, boost::parameter::void_>::node *>' requested here
        node * n = pool.template construct<true, Bounded>(t, pool.null_handle());


/home/uppi/lib/include/boost/lockfree/queue.hpp:270:16: note: in instantiation of function template specialization 'boost::lockfree::queue<PacketMessage,
      boost::lockfree::allocator<boost::interprocess::allocator<PacketMessage, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family,
      boost::interprocess::offset_ptr<void, long, unsigned long, 0>, 0>, iset_index> > >, boost::parameter::void_, boost::parameter::void_>::do_push<true>' requested here
        return do_push<true>(t);


src/model/Queue.inl:4:18: note: in instantiation of member function 'boost::lockfree::queue<PacketMessage, boost::lockfree::allocator<boost::interprocess::allocator<PacketMessage,
      boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void, long, unsigned long, 0>, 0>, iset_index> > >,
      boost::parameter::void_, boost::parameter::void_>::bounded_push' requested here
        return m_queue->bounded_push(message);                         

/home/uppi/lib/include/boost/interprocess/offset_ptr.hpp:450:4: note: candidate function
   operator unspecified_bool_type() const

请告诉我我漏掉了什么。

你问过lockfree的作者Tim Blechmann了吗?他通常很乐意帮忙。 - eile
谢谢,我刚刚给他发送了一封电子邮件。 - uppi
1个回答

15

使用在共享内存中的 boost::lockfree::queueboost::lockfree::stack 的元素数量被限制在65535个,这是出于兼容性原因。如果你只有单生产者、单消费者的情况,你可能想要使用boost::lockfree::spsc_queue,但该队列也不支持动态调整大小。

造成这种限制的原因是32位兼容性。对于64位平台,可以尝试将 boost.lockfree 代码适配为使用32位而不是16位索引,但需要进行一些较大的更改才能正确实现。


@timblechmann但是,如果平台x86_64已经存在10年了,为什么还没有实现boost :: lockfree :: queue的64位实现呢? - Alex
@Alex,动手实践并提供一个补丁来添加一个配置数据结构的策略吧 ;) - timblechmann

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接