【问题标题】:Using boost::lockfree::spsc_queue with an allocator将 boost::lockfree::spsc_queue 与分配器一起使用
【发布时间】:2013-11-01 15:00:19
【问题描述】:

以下是我的问题的表示。

#include <boost/lockfree/spsc_queue.hpp>

class test {
  struct complicated {
    int x;
    int y;   
  };
  std::allocator<complicated> alloc;
  boost::lockfree::spsc_queue<complicated, 
    boost::lockfree::allocator<std::allocator<complicated> > > spsc;
  test(void);

}; 

test::test(void): spsc( alloc ) {};

使用此代码,VS2010 出现以下错误:

错误 C2512:'boost::lockfree::detail::runtime_sized_ringbuffer':没有合适的默认构造函数可用

在编译类模板成员函数'boost::lockfree::spsc_queue::spsc_queue(const std::allocator<_ty> &)'时

错误消息表明它正在编译一个带有一个参数的构造函数,我认为它应该是分配器,但主要错误是关于默认构造函数。

文档的起点是http://www.boost.org/doc/libs/1_54_0/doc/html/lockfree.html

用 boost::lockfree::allocator 定义 boost::lockfree::spsc_queue 的适当机制是什么?

【问题讨论】:

    标签: c++ boost


    【解决方案1】:

    根据 boost 源代码,由于您没有为 spsc_queue 指定编译时容量,spsc_queue 的基类通过 typedefs 和模板魔术解析为具有以下构造函数的 runtime_sized_ringbuffer

    explicit runtime_sized_ringbuffer(size_t max_elements);
    
    template <typename U>
    runtime_sized_ringbuffer(typename Alloc::template rebind<U>::other const & alloc, size_t max_elements);
    
    runtime_sized_ringbuffer(Alloc const & alloc, size_t max_elements);
    

    如您所见,所有这些构造函数都需要一个max_element 参数。提供它的唯一方法是使用以下spsc_queue 构造函数之一:

    explicit spsc_queue(size_type element_count):
        base_type(element_count)
    {
        BOOST_ASSERT(runtime_sized);
    }
    
    template <typename U>
    spsc_queue(size_type element_count, typename allocator::template rebind<U>::other const & alloc):
        base_type(alloc, element_count)
    {
        BOOST_STATIC_ASSERT(runtime_sized);
    }
    
    spsc_queue(size_type element_count, allocator_arg const & alloc):
        base_type(alloc, element_count)
    {
        BOOST_ASSERT(runtime_sized);
    }
    

    换句话说,在调用spsc_queue 构造函数时,尝试提供大小以及分配器。

    【讨论】:

    • 好的,谢谢。我将不得不更多地研究 runtime_sized_ringbuffer 以了解 element_count/max_elements 的用途,即它是起始计数还是最大计数。因为如果它是最大计数,那么我现在确实不需要我可以理解的分配器。
    • 根据代码,它确实是元素的最大数量。您可能想尝试boost::lockfree::spsc_queue&lt;complicated, boost::lockfree::capacity&lt;20 /*or some other number*/&gt; &gt;。我认为这将设置您的队列以使用编译时大小的环形缓冲区。
    猜你喜欢
    • 2015-01-09
    • 2017-10-23
    • 1970-01-01
    • 2015-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-27
    相关资源
    最近更新 更多