Thread: C++17, VS 2019: scoped_lock mutex bug or error on my part?

  1. #1
    Registered User Chris87's Avatar
    Join Date
    Dec 2007
    Posts
    139

    C++17, VS 2019: scoped_lock mutex bug or error on my part?

    I've been trying to use scoped_lock in VS2019 when trying to make a thread safe database class that uses an underlying queue, also thread-safe, to execute operations fed into it. It's a basic class compared to what I've seen out there, but I can't overcome this one error to see if it's reliable or not:

    Code:
    Severity	Code	Description	Project	File	Line	Suppression State
    Error	C2662	'void std::_Mutex_base::unlock(void)': cannot convert 'this' pointer from '_Mutex' to 'std::_Mutex_base &'	cmmnet	C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30037\include\mutex	488
    The output log is even more confusing:
    Code:
    1>tssqlite.cpp
    1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30037\include\mutex(488,1): error C2662: 'void std::_Mutex_base::unlock(void)': cannot convert 'this' pointer from '_Mutex' to 'std::_Mutex_base &'
    1>        with
    1>        [
    1>            _Mutex=const std::mutex
    1>        ]
    1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30037\include\mutex(488,9): message : Conversion loses qualifiers
    1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30037\include\mutex(66,10): message : see declaration of 'std::_Mutex_base::unlock'
    1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30037\include\mutex(487): message : while compiling class template member function 'std::scoped_lock<const std::mutex>::~scoped_lock(void) noexcept'
    1>C:\Users\admin\Documents\GitHub\network-interface\src\tssqlite.cpp(45): message : see reference to function template instantiation 'std::scoped_lock<const std::mutex>::~scoped_lock(void) noexcept' being compiled
    1>C:\Users\admin\Documents\GitHub\network-interface\src\tssqlite.cpp(45): message : see reference to class template instantiation 'std::scoped_lock<const std::mutex>' being compiled
    1>tsmysql.cpp
    The class contains a std::mutex which I only use like this. It works in the thread safe wrapper class for my queue, but not here?
    Code:
    int TSSQLite::GetLastResult() const
    {
    	std::scoped_lock lock(m_mutex);
    	return m_current.result;
    }
    Whereas MSVC has no issue with this:
    Code:
    template <class T>
    const T& TSQueue<T>::Front()
    {
    	std::scoped_lock lock(m_mutex);
    	return m_queue.front();
    }

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,127
    "Conversion loses qualifiers" suggests that the "const" is causing the difference.
    The best argument against democracy is a five minute conversation with the average voter. - Churchill

  3. #3
    Registered User Chris87's Avatar
    Join Date
    Dec 2007
    Posts
    139
    Thanks, I'll check to see if I did a const goof-up

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,658
    Perhaps your class mutex needs to be mutable.
    cv (const and volatile) type qualifiers - cppreference.com
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 03-13-2013, 12:28 PM
  2. boost::mutex error
    By nimitzhunter in forum C++ Programming
    Replies: 4
    Last Post: 05-01-2011, 12:37 PM
  3. linker 2019 issues
    By werdy666 in forum C++ Programming
    Replies: 3
    Last Post: 02-25-2009, 04:12 AM
  4. February 1, 2019...
    By Cheeze-It in forum A Brief History of Cprogramming.com
    Replies: 98
    Last Post: 08-03-2002, 07:24 AM

Tags for this Thread