Reputation: 20746
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?
Upvotes: 9
Views: 4315
Reputation: 628
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;
}
Upvotes: 2
Reputation: 393064
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;
}
Upvotes: 10