Mutex scoped_lock compile errors
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 ? Thanks, Darren Hart dvhart@byu.edu
logger.cpp:36: error: no matching function for call to ` boost::detail::thread::scoped_lockboost::mutex::scoped_lock(const boost::mutex&)'
If the mutex is a member variable, then in the const method, it is a const mutex. scoped_lock requires a non-const mutex. This is what is causing the error. Declare the mutex as mutable so it appears as non-const even in const members. (I don't know if this is the preferred way, but it is the way I currently use). It works if the mutex is declared in the .cpp because it is not part of the class, and therefore isn't const when accessed inside a const method. HTH Russell
That did the trick, many thanks. So why is it that the error talked about argument types when it *should* have reported me attempting to change a member variable within a const context ? (I admit to feeling a bit abashed at the problem however :-) ) Thanks for the time, Darren Hart dvhart@byu.edu On Tue, 2003-06-10 at 01:43, Russell Hind wrote:
logger.cpp:36: error: no matching function for call to ` boost::detail::thread::scoped_lockboost::mutex::scoped_lock(const boost::mutex&)'
If the mutex is a member variable, then in the const method, it is a const mutex. scoped_lock requires a non-const mutex. This is what is causing the error.
Declare the mutex as mutable so it appears as non-const even in const members. (I don't know if this is the preferred way, but it is the way I currently use).
It works if the mutex is declared in the .cpp because it is not part of the class, and therefore isn't const when accessed inside a const method.
HTH
Russell
Info: http://www.boost.org Wiki: http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl Unsubscribe: mailto:boost-users-unsubscribe@yahoogroups.com
Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
Darren Vincent Hart wrote:
That did the trick, many thanks. So why is it that the error talked about argument types when it *should* have reported me attempting to change a member variable within a const context ? (I admit to feeling a bit abashed at the problem however :-) )
The compiler is correct with its error (whether it is a helpful diagnostic or not). You are passing a const mutex to an method that expects a mutex. Its like passing an int to method that expects a string. const mutex is a different type than mutex. So that is why the error is about argument types. You weren't actually attempting to modify a const object yourself was, but scoped_lock explicitly states that it wants a 'mutex', not a 'const mutex' so that is the error the compiler was displaying C++ compiler errors aren't always the most helpful, but they are logical (usually) Russell
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
participants (3)
-
Darren Vincent Hart
-
Russell Hind
-
William E. Kempf