Cannot use boost::shared_mutex

2.6k Views Asked by At

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.

2

There are 2 best solutions below

0
On BEST ANSWER

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.

3
On

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.