Search code examples
c++boostboost-mutex

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.


Solution

  • 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.