I would like to lock the keys/index in another map like this:
std::map<int, boost::mutex> pointCloudsMutexes_;
pointCloudsMutexes_[index].lock();
However, I am getting the following error:
/usr/include/c++/4.8/bits/stl_pair.h:113: error: no matching function for call to 'boost::mutex::mutex(const boost::mutex&)'
: first(__a), second(__b) { }
^
It seems to work with std::vector
, but not with std::map
. What am I doing wrong?
Map require a copy constructor,but unfortunately
boost::mutex
has no public copy constructor. Mutex declared as below:I don't think vector works either, it should have same problem. Can you
push_back
anboost::mutex
into vector?In C++ before C++11, the mapped type of a
std::map
must be both default-constructible and copy-constructible, when callingoperator[]
. However,boost::mutex
is explicitly designed not to be copy-constructible, because it is generally unclear what the semantics of copying a mutex should be. Due toboost::mutex
not being copyable, insertion of such value usingpointCloudsMutexes_[index]
fails to compile.The best workaround is to use some shared pointer to
boost::mutex
as the mapped type, e.g:PS: C++11 removed the requirement for the mapped type to be copy-constructible.