Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boost mutex strange error with private member

Tags:

c++

boost

I have a strange error.

class INST
{
public:
boost::mutex m_mutex;
};

std::vector<INST> m_inst;

error C2248: 'boost::mutex::mutex' : cannot access private member declared in class 'boost::mutex' see declaration of 'boost::mutex::mutex'

However, my other class is fine,

class VIEW
{
public:
boost::mutex m_mutex;
};

VIEW m_view;

Am I missing something here? I have tried to declare m_mutex to private, but still have the same problem.

Thanks.

like image 868
2607 Avatar asked Feb 14 '12 21:02

2607


2 Answers

mutexes can't be copied, so you can't place them in a container which would copy the mutex. The error is likely referring to the private copy constructor of the mutex.

like image 139
nos Avatar answered Oct 19 '22 20:10

nos


I realize that this question is really old, but I stumbled upon the same problem earlier today and Google lead me here. However, the proposed solution did not suit me so I wanted to describe how I solved it in my own project.

I have a vector of classes just like you, and I manage these in such a way so that once access to the members of the vector begins, the vector will never be resized again. I do want the ability to resize the vector a few times at the start, though, before processing begins. I also wanted to allow the threads to operate on any of the items in the vector in a random access fashion.

I solved the problem with the mutex by allocating it dynamically in the constructor of the class, and destroying it in the destructor. Naturally, if you do this you must guarantee that nobody is waiting on the mutex when you delete it. This solution works for me because I never copy objects out of the vector, I only access them inside of the container.

like image 28
Philip Bennefall Avatar answered Oct 19 '22 21:10

Philip Bennefall