Sunday, 6 September 2020

How to Solve mutex in Namespace std does not Name a Type

Setting up the environment for Mingw-w64 may look easy and straightforward, but choosing the wrong setting during the setup may lead to failure to build or compile the source code. For example, if the source code contains the usage of mutex. With the wrong installation settings for Mingw-w64, you may stumble upon similar errors that are shown below.

error: 'mutex' in namespace 'std' does not name a type

     std::mutex m_mutex;

 

note: 'std::mutex' is defined in header '<mutex>'; did you forget to '#include <mutex>'?

 

error: 'mutex' is not a member of 'std'

         std::lock_guard<std::mutex> lock(m_mutex);

 

error: template argument 1 is invalid

         std::lock_guard<std::mutex> lock(m_mutex);

 

error: 'm_mutex' was not declared in this scope

         std::lock_guard<std::mutex> lock(m_mutex);

 

note: suggested alternative: 'uv_mutex_t'

         std::lock_guard<std::mutex> lock(m_mutex);

                                          ^~~~~~~

                                          uv_mutex_t

 

 warning: unused variable 'lock' [-Wunused-variable]

         std::lock_guard<std::mutex> lock(m_mutex);


This is due to the settings that you specified during the installation doesn't contain the mutex implementation. So to fix this, you need to reinstall with the correct setting, where the mutex implementation is in the posix. Rerun the Mingw-w64 installer and make sure the posix is selected for the threads and continue with the installation.


Do take note to update the %PATH% Environment Variables to the newly installed path of Mingw-w64 with posix and restart your command prompt session to ensure the command is picking up the correct settings.




1 comment: