Darren Vincent Hart said:
Esteemed Boost Gurus,
I am trying to compile a method with a scoped_lock call. If I use a mutex object declared in the class implementation file (logger.cpp) it builds, if I use a mutex of the same type declared as a private member of the logger class (in logger.h), I get the following compiler error:
logger.cpp: In member function `void pel::logger::info(const std::string&, const std::string&, int, const std::string&) const': logger.cpp:36: error: no matching function for call to ` boost::detail::thread::scoped_lockboost::mutex::scoped_lock(const boost::mutex&)' /usr/include/boost/thread/detail/lock.hpp:62: error: candidates are: boost::detail::thread::scoped_lockboost::mutex::scoped_lock(const boost::detail::thread::scoped_lockboost::mutex&) /usr/include/boost/thread/detail/lock.hpp:67: error: boost::detail::thread::scoped_lock<Mutex>::scoped_lock(Mutex&, bool = true) [with Mutex = boost::mutex] make: *** [logger.o] Error 1
The relevant method from the implementation file is as follows:
void logger::info(const std::string& msg, const std::string& file, int line, const std::string& function) const { // io_mutex is declared above in the pel namespace boost::mutex::scoped_lock scoped_lock(io_mutex); // no problem // mutex is a private member of the logger class boost::mutex::scoped_lock scoped_lock2(mutex); // line 36, fails std::cout << "...blah..." << std::endl; }
Can someone explain why these are treated differently by the compiler ?
It's a cv problem. The lock must take a non-const reference, and you're in a const method so the reference is const. Declare your mutex to be mutable. This is in the FAQ. -- William E. Kempf