5

I have this code:

////
// Default Namespaces
///

using namespace std;

typedef map <string, boost::shared_mutex>   t_map_shared_mutex;

int main(int argc, char** argv) {

    t_map_shared_mutex  list_lock;

    boost::shared_mutex global_lock;

    string          i = "ABC";

    boost::unique_lock < boost::shared_mutex > l_lock ( global_lock );

    boost::unique_lock < boost::shared_mutex > lock ( list_lock[i] );
        //Do Something with that lock
    lock.unlock();

    l_lock.unlock();

}

This generates the following error. From what I understand (and I could be pretty wrong here) g++ is telling me that the mutex is being passed as a const value... And I can't see why.

In file included from /usr/include/c++/4.4/utility:63,
                 from /usr/include/boost/config/no_tr1/utility.hpp:21,
                 from /usr/include/boost/config/select_stdlib_config.hpp:33,
                 from /usr/include/boost/config.hpp:40,
                 from /usr/include/boost/date_time/compiler_config.hpp:12,
                 from /usr/include/boost/date_time/posix_time/posix_time.hpp:14,
                 from prova.cpp:5:
/usr/include/c++/4.4/bits/stl_pair.h: In constructor ‘std::pair<_T1, _T2>::pair(const _T1&, const _T2&) [with _T1 = const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _T2 = boost::shared_mutex]’:
/usr/include/c++/4.4/bits/stl_map.h:450:   instantiated from ‘_Tp& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const _Key&) [with _Key = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Tp = boost::shared_mutex, _Compare = std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, _Alloc = std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::shared_mutex> >]’
prova.cpp:66:   instantiated from here
/usr/include/c++/4.4/bits/stl_pair.h:84: error: no matching function for call to ‘boost::shared_mutex::shared_mutex(const boost::shared_mutex&)’
/usr/include/boost/thread/pthread/shared_mutex.hpp:47: note: candidates are: boost::shared_mutex::shared_mutex()
/usr/include/boost/thread/pthread/shared_mutex.hpp:21: note:                 boost::shared_mutex::shared_mutex(boost::shared_mutex&)
/usr/include/c++/4.4/bits/stl_pair.h: In constructor ‘std::pair<_T1, _T2>::pair(const std::pair<_U1, _U2>&) [with _U1 = const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _U2 = boost::shared_mutex, _T1 = const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _T2 = boost::shared_mutex]’:
/usr/include/c++/4.4/ext/new_allocator.h:105:   instantiated from ‘void __gnu_cxx::new_allocator<_Tp>::construct(_Tp*, const _Tp&) [with _Tp = std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::shared_mutex>]’
/usr/include/c++/4.4/bits/stl_tree.h:371:   instantiated from ‘std::_Rb_tree_node<_Val>* std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_create_node(const _Val&) [with _Key = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Val = std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::shared_mutex>, _KeyOfValue = std::_Select1st<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::shared_mutex> >, _Compare = std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, _Alloc = std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::shared_mutex> >]’
/usr/include/c++/4.4/bits/stl_tree.h:881:   instantiated from ‘std::_Rb_tree_iterator<_Val> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_(const std::_Rb_tree_node_base*, const std::_Rb_tree_node_base*, const _Val&) [with _Key = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Val = std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::shared_mutex>, _KeyOfValue = std::_Select1st<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::shared_mutex> >, _Compare = std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, _Alloc = std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::shared_mutex> >]’
/usr/include/c++/4.4/bits/stl_tree.h:1215:   instantiated from ‘std::_Rb_tree_iterator<_Val> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique_(std::_Rb_tree_const_iterator<_Val>, const _Val&) [with _Key = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Val = std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::shared_mutex>, _KeyOfValue = std::_Select1st<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::shared_mutex> >, _Compare = std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, _Alloc = std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::shared_mutex> >]’
/usr/include/c++/4.4/bits/stl_map.h:540:   instantiated from ‘typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key, _Tp> >, _Compare, typename _Alloc::rebind<std::pair<const _Key, _Tp> >::other>::iterator std::map<_Key, _Tp, _Compare, _Alloc>::insert(typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key, _Tp> >, _Compare, typename _Alloc::rebind<std::pair<const _Key, _Tp> >::other>::iterator, const std::pair<const _Key, _Tp>&) [with _Key = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Tp = boost::shared_mutex, _Compare = std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, _Alloc = std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::shared_mutex> >]’
/usr/include/c++/4.4/bits/stl_map.h:450:   instantiated from ‘_Tp& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const _Key&) [with _Key = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Tp = boost::shared_mutex, _Compare = std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, _Alloc = std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::shared_mutex> >]’
prova.cpp:66:   instantiated from here
/usr/include/c++/4.4/bits/stl_pair.h:101: error: no matching function for call to ‘boost::shared_mutex::shared_mutex(const boost::shared_mutex&)’
/usr/include/boost/thread/pthread/shared_mutex.hpp:47: note: candidates are: boost::shared_mutex::shared_mutex()
/usr/include/boost/thread/pthread/shared_mutex.hpp:21: note:                 boost::shared_mutex::shared_mutex(boost::shared_mutex&)

How to solve that ?

ElementalStorm
  • 809
  • 12
  • 31

2 Answers2

11

STL containers require a copy constructor for the value type; since mutexes can't be copied, you can't put them directly into a map. You must use a pointer to a mutex (perhaps via std::unique_ptr or boost::ptr_map) instead.

Mat
  • 202,337
  • 40
  • 393
  • 406
bdonlan
  • 224,562
  • 31
  • 268
  • 324
  • Maybe my skill with boost mutexes is not so strong... How would you do that ? Can you give me some code ? – ElementalStorm Aug 30 '11 at 17:32
  • 2
    @St0rM - I'd say its more your skill with the standard containers that needs work. There are some situations where it is OK to put objects directly into a container if you know what you are doing. But in general you should be looking to **put pointers to objects into a container** (like a std::map), **not the objects themsevles**. Containers can copy around objects internally as they see fit, so you really have no control over when constructors and destructors are going to be called. – T.E.D. Aug 30 '11 at 17:35
  • 3
    @TED: You don't really mean in general, do you? Pointers in containers isn't usually correct. – GManNickG Aug 30 '11 at 17:44
  • @TED : I agree strongly with GMan -- in general you should put objects into a container, not pointers to objects. – ildjarn Aug 30 '11 at 18:37
  • I'd add the qualification that although raw pointers should be avoided, RAII containers (such as `std::unique_ptr`) are fine, and failing that you can use a pointer-aware container such as `ptr_map` - the idea is to ensure deallocation is automatic when the container is destroyed – bdonlan Aug 30 '11 at 18:58
  • 1
    @bdonlan - ..as long as you are fine with whatever happens when your objects get copied around happening possibly a lot behind the scenes on you, then yes. I work a lot in realtime realms where it is generally *not* OK. If a "copy" is liable to close out and reacquire a resource on me (eg: a mutex, as above), then putting it in a container can be a huge waste, if not a flat out error. The boost folks were wise enough to realise this and make their mutexes uncopyable, but not every internal or third-party developer is going to be that smart for every object (and some may want expensive copies). – T.E.D. Aug 30 '11 at 21:29
  • ...I'll admit GMan's "general case" may be very different than mine though. :-) – T.E.D. Aug 30 '11 at 21:37
  • @TED : I assume your opinion must change somewhat given a compiler with support for rvalue references and move semantics, no? – ildjarn Aug 30 '11 at 23:23
  • @ildjarn - It would certianly help a bit, no doubt. But I'd have to *know* how the object in question's copy is implemented. For now, it really isn't that hard to just chuck pointers into the container. As long as you make sure the objects (mutices) have a longer lifetime than the container, there is really no problem. Wrapping them all in an object with proper constructors and destructors is generally enough to take care of this. – T.E.D. Sep 02 '11 at 13:32
0

It seems to me as std::map is trying to call the copy constructor of boost::shared_mutex by passing it a const boost::shared_mutex&, but no such constructor is available:

/usr/include/c++/4.4/bits/stl_pair.h:84: error: no matching function for call to ‘boost::shared_mutex::shared_mutex(const boost::shared_mutex&)’
/usr/include/boost/thread/pthread/shared_mutex.hpp:47: note: candidates are: boost::shared_mutex::shared_mutex()
/usr/include/boost/thread/pthread/shared_mutex.hpp:21: note:                 boost::shared_mutex::shared_mutex(boost::shared_mutex&)
Andrea Bergia
  • 5,502
  • 1
  • 23
  • 38