Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to define a vector<boost::mutex> in C++ ?

I want to define a vector with boost::mutex like :

  boost::mutex myMutex ;
  std::vector< boost::mutex > mutexVec; 
  mutexVec.push_back(myMutex); 

But, I got error on Linux:

/boost_1_45_0v/include/boost/thread/pthread/mutex.hpp:33: error: âboost::mutex::mutex(const boost::mutex&)â is private /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/ext/new_allocator.h:104: error: within this context

I cannot find a solution by searching online.

thanks

like image 241
user1002288 Avatar asked Nov 24 '11 06:11

user1002288


2 Answers

You could use a boost pointer container:

#include <boost/thread.hpp>
#include <boost/ptr_container/ptr_vector.hpp>

boost::ptr_vector<boost::mutex> pv;
pv.push_back(new boost::mutex);

A ptr_vector takes ownership of its pointers so that they are deleted when appropriate without any of the overhead a smart pointer might introduce.

like image 200
Ferruccio Avatar answered Oct 06 '22 00:10

Ferruccio


The copy constructor is private. You aren't supposed to copy a mutex.

Instead use:

boost::mutex *myMutex = new boost::mutex();
std::vector< boost::mutex *> mutexVec; 
mutexVec.push_back(myMutex);

and if you don't want to manage the memory yourself use a boost::shared_ptr<boost::mutex> instead of boost::mutex*

like image 28
PeterT Avatar answered Oct 05 '22 22:10

PeterT