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

This is a discussion on g++ 4.7.2 error: 'mutex' in namespace 'std' does not name a type within the C++ Programming forums, part of the General Programming Boards category; I've verified this on ubuntu 12.10 and on windows/mingw, and found that g++ version 4.7.2 seems to have broken thread/mutex ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    2,292

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

    I've verified this on ubuntu 12.10 and on windows/mingw, and found that g++ version 4.7.2 seems to have broken thread/mutex support. has anyone else observed this?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    Can you post your test case?
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,167
    has anyone else observed this?
    I usually build my own package for "Windows", so maybe it is just my builds script, but that seems par for the course under "MinGW".

    I can't speak to anything about "Ubuntu".

    My understanding, and only that, is that a lot of work needs to be done for "libstdc++" before the implementation has the proper safety the respect to the standard. You may not, and probably will not, be able to match your own expectations with "libstdc++" for a bit longer.

    Soma

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    2,292
    Quote Originally Posted by Salem View Post
    Can you post your test case?
    this was my actual test case
    Code:
    #include <iostream>
    #include <thread>
    #include <vector>
    #include <memory>
    
    std::mutex mutex;
    int mutexProtected = 0;
    
    void threadProc()
    {
        for (int i = 0; i < 5; ++i)
        {
    	std::lock_guard<std::mutex> lock(mutex);
    	std::cout << "Value: " << ++mutexProtected << std::endl;
        }
    }
    
    int main()
    {
        std::vector<std::shared_ptr<std::thread> > threadVector;
    
        for (int i = 0; i < 20; ++i)
        {
    	threadVector.push_back(std::shared_ptr<std::thread>(new std::thread(threadProc)));
        }
    
        for (auto& thread : threadVector)
        {
    	thread->join();
        }
    
        return 0;
    }
    my compile command is
    Code:
    g++ -pthread -std=c++0x -c main.cpp
    and the output I get on mingw is
    Code:
    main.cpp:6:1: error: 'mutex' in namespace 'std' does not name a type
    main.cpp: In function 'void threadProc()':
    main.cpp:13:2: error: 'lock_guard' is not a member of 'std'
    main.cpp:13:18: error: 'mutex' is not a member of 'std'
    main.cpp:13:35: error: 'mutex' was not declared in this scope
    main.cpp:13:40: error: 'lock' was not declared in this scope
    main.cpp: In function 'int main()':
    main.cpp:22:33: error: 'thread' is not a member of 'std'
    main.cpp:22:33: error: 'thread' is not a member of 'std'
    main.cpp:22:44: error: template argument 1 is invalid
    main.cpp:22:46: error: template argument 1 is invalid
    main.cpp:22:46: error: template argument 2 is invalid
    main.cpp:22:60: error: invalid type in declaration before ';' token
    main.cpp:26:15: error: request for member 'push_back' in 'threadVector', which is of non-class type 'int'
    main.cpp:26:41: error: 'thread' is not a member of 'std'
    main.cpp:26:41: error: 'thread' is not a member of 'std'
    main.cpp:26:52: error: template argument 1 is invalid
    main.cpp:26:58: error: expected type-specifier
    main.cpp:26:58: error: expected ')'
    main.cpp:29:25: error: no matching function for call to 'begin(int&)'
    more errors snipped...
    even a simple test case like this exhibits essentially the same behavior
    Code:
    #include <mutex>
    
    int main()
    {
        std::mutex mutex;
        return 0;
    }
    there were many more errors, but they were all related to the compiler's inability to find a begin() or end() function for std::vector<std::shared_ptr<std::thread> >.

    after more digging, I determined that on ubuntu, modifying the code to include <mutex> solved the problem. it seems that <mutex> was included implicitly by <thread> in g++ 4.6.3 on fedora 15, and a recent move to ubuntu 12.10 (gcc 4.7.3) for my main dev machine exposed that fact.

    on mingw (gcc 4.7.3), including <mutex> did not solve the problem. it appears that the std::thread implementation in the official release of mingw version of g++ 4.7.3 is incomplete or nonexistent.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    2,693
    You may want to include <mutex> to use the mutex class. After I add that include file your program compiles without errors for me. But I do use the -std=c++11 flag instead of -std=c++0x.

    Jim

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    2,292
    Quote Originally Posted by jimblumberg View Post
    You may want to include <mutex> to use the mutex class. After I add that include file your program compiles without errors for me.
    as I said in my last post, that fixes it on ubuntu, but not on windows.

    Quote Originally Posted by jimblumberg View Post
    But I do use the -std=c++11 flag instead of -std=c++0x.
    changing -std=c++0x to -std=c++11 makes no difference. my guess is that they are equivalent.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    2,693
    changing -std=c++0x to -std=c++11 makes no difference. my guess is that they are equivalent.

    Not on my system. If I change to -std=c++0x I get these errors:

    /usr/include/c++/4.7/bits/c++0x_warning.h|32|error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.|
    main.cpp||In function ‘int main()’:|
    main.cpp|5|error: ‘mutex’ is not a member of ‘std’|
    main.cpp|5|error: expected ‘;’ before ‘mutex’|
    ||=== Build finished: 3 errors, 0 warnings ===|
    Jim

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    2,292
    Quote Originally Posted by jimblumberg View Post
    changing -std=c++0x to -std=c++11 makes no difference. my guess is that they are equivalent.

    Not on my system. If I change to -std=c++0x I get these errors:



    Jim
    what actual version of gcc is it? (gcc --version)

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    2,693
    Version 4.7.2

    Jim

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    2,292
    that's interesting. both produce identical results on my ubuntu and mingw systems with 4.7.3.

  11. #11
    Registered User
    Join Date
    May 2009
    Posts
    2,508
    In my Code::Blocks with MinGW GCC (gcc version 4.7.1 (tdm-1)),
    _GLIBCXX_HAS_GTHREADS is not defined.

    From mutex header.

    Code:
    #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1)
    
    namespace std _GLIBCXX_VISIBILITY(default)
    {
    I am guessing that there is a option that compiles MinGW GCC with this _GLIBCXX_HAS_GTHREADS defined.

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  12. #12
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    681
    MinGW-w64 does support C++11 threads, and despite its name, it is also for 32-bit (which I personally use):
    GCC for both x64 & x86 Windows! - MinGW-w64
    I never put signature, but I decided to make an exception.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. boost::mutex error
    By nimitzhunter in forum C++ Programming
    Replies: 4
    Last Post: 05-01-2011, 12:37 PM
  2. namespace error
    By siddarameshwara in forum C++ Programming
    Replies: 4
    Last Post: 11-12-2010, 05:07 PM
  3. Weird Namespace error..
    By Nebbuchadnezzar in forum C++ Programming
    Replies: 10
    Last Post: 05-12-2006, 11:53 PM
  4. using namespace std error
    By Kayoss in forum C++ Programming
    Replies: 0
    Last Post: 04-26-2006, 01:56 PM
  5. using namespace error
    By johnd in forum Windows Programming
    Replies: 12
    Last Post: 08-30-2001, 07:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21