在分配器中使用boost :: lockfree :: spsc_queue [英] Using boost::lockfree::spsc_queue with an allocator

查看:127
本文介绍了在分配器中使用boost :: lockfree :: spsc_queue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的问题的代表.

The following is a representation of my problem.

#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出现以下错误:

With this code I have the following error with VS2010:

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

error C2512: 'boost::lockfree::detail::runtime_sized_ringbuffer' : no appropriate default constructor available

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

while compiling class template member function 'boost::lockfree::spsc_queue::spsc_queue(const std::allocator<_Ty> &)'

错误消息指出它正在使用一个参数编译一个构造函数,我认为应该是分配器,但是主要错误是关于默认构造函数的.

The error message states it is compiling a constructor with one argument, which I believe should be the allocator, but the primary error talks about a default constructor.

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

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

What is the appropriate mechanism for defining boost::lockfree::spsc_queue with boost::lockfree::allocator?

推荐答案

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

According to the boost source code, since you are not specifying a compile-time capacity for your spsc_queue the base class of spsc_queue gets resolved through typedefs and template magic to runtime_sized_ringbuffer which has the following constructors:

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构造函数之一:

As you can see, all these constructors expect a max_element parameter. The only way to provide that is to use one of the following spsc_queue constructors:

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构造函数时,尝试提供一个大小以及分配器.

In other words, try providing a size along with your allocator when calling your spsc_queue constructor.

这篇关于在分配器中使用boost :: lockfree :: spsc_queue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆