Violet Giraffe
Violet Giraffe

Reputation: 33579

Cannot use boost::shared_mutex

I have a small template class with a non-static member of type boost::shared_mutex. Whenever I try to compile it, I get the error:

'boost::shared_mutex::shared_mutex' : cannot access private member declared in class 'boost::shared_mutex'.

boost::shared_mutex really has a private nested class shared_mutex, but I don't understand why this problem arose.

Here's my class:

#include <boost/thread.hpp>
#include <boost/thread/shared_mutex.hpp>
#include <queue>

template <typename T>
class CThreadSafeQueue
{
public:
    CThreadSafeQueue();

private:
    boost::mutex    _sharedMutex;
    std::queue<T>   _queue;
};

template <typename T>
CThreadSafeQueue<T>::CThreadSafeQueue()
{

}

The same happens with a regular `boost::mutex'.

I have another, non-template class, in which I have no problem using either mutex type.

Upvotes: 1

Views: 2677

Answers (2)

Violet Giraffe
Violet Giraffe

Reputation: 33579

Huh! The solution to my problems turned out to be very simple yet very hard to find. I was only having problems with methods declared const, because lockers are mutating mutexes. All I had to do was make it mutable.

Upvotes: -1

tune2fs
tune2fs

Reputation: 7705

You need to make the class noncopyable, or implement your own copy and assignment operator. boost::mutex is non copyable, therefore you get this error.

You can derive your class from boost::noncopyable, to make it noncopyable.

Upvotes: 3

Related Questions