Raymond Burkholder
Raymond Burkholder

Reputation: 504

Using boost::lockfree::spsc_queue with an allocator

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 ) {};

With this code I have the following error with VS2010:

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

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.

Starting point of documentation is at http://www.boost.org/doc/libs/1_54_0/doc/html/lockfree.html.

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

Upvotes: 2

Views: 3275

Answers (1)

user955279
user955279

Reputation:

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);

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);
}

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

Upvotes: 2

Related Questions