C++ mutex in namespace std does not name a type

前端 未结 11 905
日久生厌
日久生厌 2020-11-28 13:22

I\'m writing a simple C++ program to demonstrate the use of locks. I am using codeblocks and gnu gcc compiler.

 #inclu         


        
相关标签:
11条回答
  • 2020-11-28 13:42

    I fixed it by following steps:

    • Project > Build option...
    • The default selected compiler: GNU GCC Compiler
    • On tab "Compiler settings / Compiler flags", check option "Have g++ follow the C++11 ISO C++ language standard [-std=c++11]"
    0 讨论(0)
  • 2020-11-28 13:43

    I got the same error with gcc4.7.7.

    After adding "-std=c++0x", it is fixed.

    0 讨论(0)
  • 2020-11-28 13:44

    I encountered this same problem when using MingW-W64 7.2.0. I tested out several different Windows builds from the mingw-64 download page, and found that MinGW-W64 GCC-8.1.0 supports mutex and contains the pthread library. When installing, I selected the following options:

    • x86_64
    • posix
    • seh

    My multi-threaded code based on pthreads now compiles and runs cleanly on both Windows and Linux with no changes.

    This version is leaner than the 7.3.0 build I was using because it doesn't have a CygWin environment or package manager. I also copied mingw32-make.exe to make.exe so my Makefile wouldn't need to be modified. The installer creates a "Run terminal" link in the Windows Start Menu.

    0 讨论(0)
  • 2020-11-28 13:51

    This has now been included in MingW (Version 2013072300). To include it you have to select the pthreads package in the MinGW Installation Manager.

    Pthreads package options from MingW Installation Manager

    0 讨论(0)
  • 2020-11-28 13:55

    Many classes of the standard thread library can be replaced with the boost ones. A very easy workaround is to change the entire standard mutex file with a couple of lines.

    #include <boost/thread.hpp>
    
    namespace std
    {
       using boost::mutex;
       using boost::recursive_mutex;
       using boost::lock_guard;
       using boost::condition_variable;
       using boost::unique_lock;
       using boost::thread;
    }
    

    And do not forget to link against boost thread library.

    0 讨论(0)
  • 2020-11-28 13:56

    Use POSIX threading model for MINGW:

    $ sudo update-alternatives --config i686-w64-mingw32-gcc
    <choose i686-w64-mingw32-gcc-posix from the list>
    
    $ sudo update-alternatives --config i686-w64-mingw32-g++
    <choose i686-w64-mingw32-g++-posix from the list>
    
    $ sudo update-alternatives --config x86_64-w64-mingw32-gcc
    <choose x86_64-w64-mingw32-gcc-posix from the list>
    
    $ sudo update-alternatives --config x86_64-w64-mingw32-g++
    <choose x86_64-w64-mingw32-g++-posix from the list>
    

    See also: mingw-w64 threads: posix vs win32

    0 讨论(0)
提交回复
热议问题