2

I'm trying to create a Queue using boost::lockfree:queue from an own declared type. I checked the Class template for queue for the requirements:

  • T must have a copy constructor
  • T must have a trivial assignment operator
  • T must have a trivial destructor

So I implemeted the following class:

namespace ek {
class SourceQueueElement{
    private:
        int id;
        std::string fileName;
    public:
        SourceQueueElement(int id, std::string fileName);
        SourceQueueElement(const SourceQueueElement &rhs);
        SourceQueueElement& operator=(const SourceQueueElement& rhs);
        ~SourceQueueElement();
        int getId();
        void setId(int id);
        std::string getFileName();
        void setFileName(std::string fileName);
};}

And implemented the methods:

#include "sourcequeueelement.h"

ek::SourceQueueElement::SourceQueueElement(int id, std::string fileName){
    this->id = id;
    this->fileName = fileName;
}

ek::SourceQueueElement::SourceQueueElement(const ek::SourceQueueElement &rhs){
    this->setFileName(rhs.getFileName());
    this->setId(rhs.getId());
}

ek::SourceQueueElement::~SourceQueueElement(){
    this->id = 0;
    this->fileName = null;
}

ek::SourceQueueElement& operator=(const ek::SourceQueueElement &rhs){
    this->setFileName(rhs.getFileName());
    this->setId(rhs.getId());
    return *this;
}

The method implementation is really simple I think. The problem is, that trying to use the class in the boost::lockfree_queue fails:

boost::lockfree::queue<ek::SourceQueueElement> sourceQueue(256);

with the following error:

/usr/include/boost/lockfree/queue.hpp:87:5: error: invalid application of 'sizeof' to incomplete type 'boost::STATIC_ASSERTION_FAILURE<false>'
 BOOST_STATIC_ASSERT((boost::has_trivial_destructor<T>::value));
 
/usr/include/boost/lockfree/queue.hpp:91:5: error: invalid application of 'sizeof' to incomplete type 'boost::STATIC_ASSERTION_FAILURE<false>'
 BOOST_STATIC_ASSERT((boost::has_trivial_assign<T>::value));

I know that according to this Stack Overflow question this error occurs if the requirements for the queue are not complied. E.g. if you try to use std::string as T for boost::lockfree:queue. But I think I have implemented the needed methods in my class. Does anybody know which is the problem or have I misinterpreted the requirements?

Thanks for your help.

Community
  • 1
  • 1
KK-Media
  • 251
  • 1
  • 8
  • 2
    Your assignment operator and destructor are [non-trivial by definition](http://stackoverflow.com/a/3899248/21475), which is what the static assertions are trying to convey. – Cameron Jan 02 '14 at 19:03
  • So did I got thsi right, the problem is that I declared a destructor and an assignment operator? If I comment out this methods the error still is reported. So the problem is the String "fileName" which does not have a trivial destructor? – KK-Media Jan 02 '14 at 19:13
  • 1
    Yep, afraid so. You can't have a trivial destructor/operator since `std::string`, at the very least, has non-trivial ones. The boost lock-free queue is somewhat limiting! (There's also a max of ~65k elements, if I'm not mistaken.) Your best option is probably to use it with raw pointers and do your own memory management elsewhere. – Cameron Jan 02 '14 at 19:24
  • Ok, thank you very much. Are there any other Queue-Implementations which are threadsafe? – KK-Media Jan 02 '14 at 19:26
  • I'm currently working on one, but it's not finished (and I'm really slow at finishing hobby projects -- I'm not sure when it'll be done). I'm sure there's a few floating around, but none that I know of that are industrial-strength (both fast and feature complete, including memory management, and that are written in C++), hence why I'm writing one ;-) – Cameron Jan 02 '14 at 19:37
  • Oh wait, I just remembered that [MSVC comes with a MPMC queue implementation](http://msdn.microsoft.com/en-us/library/ee355358.aspx). As I've never used it myself, I'm not sure of its characteristics, but it's definitely an option if you're on that platform. – Cameron Jan 02 '14 at 19:44
  • 1
    @seeberg: do you need a threat-safe queue or one which is [also] lock-free? Creating a threat-safe queue using `std::mutex` and `std::condition_variable` is fairly straight forward. – Dietmar Kühl Jan 02 '14 at 20:15

0 Answers0