2

I have a struct MyClass inside another class MyOuterClass I am trying to put into a boost::lockfree::queue. My structure looks like below

struct MyClass
{
    MyClass() {}
    MyClass(const string& topic, const string& multicastChannel, const string& netInterface, MyOuterClass::MY_COMMAND_ENUM command, MyOuterClass::CallbackType callback)
    :topic(topic), multicastChannel(multicastChannel), netInterface(netInterface), command(command), callback(callback) {}

    MyClass(const MyClass& src)
    :topic(src.topic), multicastChannel(src.multicastChannel), netInterface(src.netInterface), command(src.command), callback(src.callback) {}
    MyClass& operator=(const MyClass& src)
    {
        topic = src.topic;
        multicastChannel = src.multicastChannel;
        netInterface = src.netInterface;
        command = src.command;
        callback = src.callback;
    }

    ~MyClass(){}

    string topic;
    string multicastChannel;
    string netInterface;
    MyOuterClass::MY_COMMAND_ENUM command;
    MyOuterClass::CallbackType callback;
};

My outer class has a public member variable boost::lockfree::queue<MyClass> m_commandQueue; which throws the following error when I try to compile.

1>------ Build started: Project: MDServices, Configuration: Debug x64 ------
1>Build started 6/10/2013 4:41:00 PM.
1>InitializeBuildStatus:
1>  Touching "x64\Debug\MDServices.unsuccessfulbuild".
1>ClCompile:
1>  MyOuterClass.cpp
1>C:\local\boost_1_53_0\boost/lockfree/queue.hpp(79): error C2338: (boost::has_trivial_destructor<T>::value)
1>          src\MyOuterClass.cpp(70) : see reference to class template instantiation 'boost::lockfree::queue<T>' being compiled
1>          with
1>          [
1>              T=MDServices::MyClass
1>          ]
1>C:\local\boost_1_53_0\boost/lockfree/queue.hpp(83): error C2338: (boost::has_trivial_assign<T>::value)
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:03.38
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

I checked the thread /boost/lockfree/queue.hpp: error: static assertion failed: (boost::has_trivial_destructor<T>::value) however I am still unable to compile. What am I missing here?

Thanks.

Chinmay

Community
  • 1
  • 1
Chinmay Nerurkar
  • 495
  • 6
  • 22
  • Your linked question exactly answers your question: You can't define your own destructor or copy assignment operator. – Mark B Jun 10 '13 at 21:01
  • `trivial` means you do not define it yourself. – Jesse Good Jun 10 '13 at 21:03
  • 1
    Your `string` members might also prevent you from storing your class in the queue as it is non-trivial. – Jesse Good Jun 10 '13 at 21:11
  • @MarkB @Jesse - I tried without defining my own destructor and copy assignment operator. I still get the same error. That is when I searched for the link I have posted and wrote my own copy constructor, assignment operator and destructor. The other thread says the 'trivial' requirement is recursive. Could the use of `std::string` class causing the issue? – Chinmay Nerurkar Jun 10 '13 at 21:18
  • @JesseGood - Thanks for conforming that. Is there a way to get around that? Or do I have to use the regular queue with locks? – Chinmay Nerurkar Jun 10 '13 at 21:19
  • 1
    @ChinmayNerurkar: You could use pointers. push on the pointers with `new` and then `delete` when popping. – Jesse Good Jun 10 '13 at 21:23
  • @JesseGood - Thanks. That works well for me. I am going to take that as a solution. – Chinmay Nerurkar Jun 10 '13 at 21:32

1 Answers1

2

As per JesseGood's comments on my question, the issue was caused due to the use of std::string in MyClass which is non-trivial. The solution was to store pointers to MyClass in my boost::lockfree::queue instead of MyClass objects directly.

Chinmay Nerurkar
  • 495
  • 6
  • 22