开发者

std::map of boost::mutex with strange behaviour

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, _KeyOfVal开发者_如何学Pythonue, _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 ?


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.


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&)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜