Assertion failure in case of boost::lockfree:queue default constructor
Asked Answered
M

2

9

How can I use boost::lockfree:queue objects?

I'm trying to write an application that constructs an object of this class via default constructor, but it gives me an assertion failure inside the boost sources:

BOOST_ASSERT(has_capacity);

How can I use the default constructor for this class? Do I need to specify the size of the queue via template arguments?

Mizzle answered 26/2, 2014 at 8:48 Comment(0)
S
9

The capacity can be given statically, so it's even before the default constructor.

boost::lockfree::queue<int, boost::lockfree::capacity<50> > my_queue;

The mechanism resembles named parameters for template arguments.

See it Live On Coliru

#include <boost/lockfree/queue.hpp>
#include <iostream>

using namespace boost::lockfree;

struct X { int i; std::string s; };

int main()
{
    queue<int, boost::lockfree::capacity<50> > q;
}
Snag answered 26/2, 2014 at 9:29 Comment(2)
and if I don't want a fixed size queue?Sargeant
then you need to pass it to the constructor IIRC (you asked about the template argument method specifically, ... ): boost::lockfree::queue<int> queue(128); (e.g. docs)Snag
P
2

You can use the queue's size_type constructor instead, for example:

#include <iostream>
#include <boost/lockfree/queue.hpp>

int main() {

    boost::lockfree::queue<int> queue( 0 );

    int pushed = 4;
    int popped = 0;

    if( queue.push( pushed ) ) {
        std::cout << "Pushed " << pushed << std::endl;
    }

    if( queue.pop( popped ) ) {
        std::cout << "Popped " << popped << std::endl;
    }

    return 0;
}
Pedanticism answered 14/7, 2019 at 21:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.