40

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

 #include <iostream>
 #include <thread>
 #include <mutex>
 using namespace std;
 int x = 0; // shared variable

 void synchronized_procedure()
 {
    static std::mutex m;
    m.lock();
    x = x + 1;
    if (x < 5)
    {
       cout<<"hello";
    }
    m.unlock();

 }

int main()
{

   synchronized_procedure();
   x=x+2;
   cout<<"x is"<<x;
}

I'm getting the following error: mutex in namespace std does not name a type.

Why am I getting this error? Doesn't the compiler support use of locks?

Parker
  • 7,244
  • 12
  • 70
  • 92
arjun
  • 2,333
  • 4
  • 21
  • 21
  • 8
    If you're on Windows, the threading library isn't implemented by MinGW yet. I've been there and wished for it to be different, but at least MSVC has it working. – chris Jan 07 '13 at 07:23
  • 1
    Wow, looking around, apparently one of [these](http://code.google.com/p/mingw-builds/downloads/list) (4.7 ones only) should now have support for it. I'm currently downloading one to test it and I'll let you know if it ends up working. – chris Jan 07 '13 at 07:38
  • Well, it's not working at all for me. I don't know if you'd have better luck. – chris Jan 07 '13 at 08:15
  • 2
    Oh, hey. [This answer](http://stackoverflow.com/a/8464622/962089) actually worked! Make sure you add `-static` to the linker options. The only problem is that I can only find one with GCC 4.7.0, which means giving up some other C++11 features until a newer one is built. – chris Jan 07 '13 at 09:00
  • Possible duplicate of [mingw-w64 threads: posix vs win32](https://stackoverflow.com/questions/17242516/mingw-w64-threads-posix-vs-win32) – rogerdpack Oct 25 '18 at 04:38

10 Answers10

23

I happened to be looking at the same problem. GCC works fine with std::mutex under Linux. However, on Windows things seem to be worse. In the <mutex> header file shipped with MinGW GCC 4.7.2 (I believe you are using a MinGW GCC version too), I have found that the mutex class is defined under the following #if guard:

#if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1)

Regretfully, _GLIBCXX_HAS_GTHREADS is not defined on Windows. The runtime support is simply not there.

You may also want to ask questions directly on the MinGW mailing list, in case some GCC gurus may help you out.

EDIT: The MinGW-w64 projects provides the necessary runtime support. Check out http://mingw-w64.sourceforge.net/ and https://sourceforge.net/projects/mingw-w64/files/. Also, as 0xC0000022L pointed out, you need to download the POSIX thread version (I missed mentioning it last time).

Yongwei Wu
  • 5,292
  • 37
  • 49
  • 6
    Turns out even with MinGW64 you have to use the GCC-related tools suffixed `-posix` as opposed to `-win32` ... which apparently refers to the threading model used. – 0xC0000022L May 29 '19 at 10:33
22

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

Amir Saniyan
  • 13,014
  • 20
  • 92
  • 137
  • 2
    Tested good on debian buster. This is exactly what I needed. The `-std=c++0x` solution is now obsolete as it's now unnecessary (at least as of gcc 8.3). – Stewart Jun 24 '19 at 12:35
  • This seems to be the correct answer. – vy32 Oct 02 '21 at 18:23
  • this solution worked for me, thank you. I just needed the last command `sudo update-alternatives --config x86_64-w64-mingw32-g++` – Zion Jun 19 '23 at 02:26
13

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

IsakBosman
  • 1,453
  • 12
  • 19
  • Please provide system information including you MingW version so it can help others – IsakBosman Aug 14 '18 at 15:26
  • 5
    Sorry: Windows, mingw32-pthreads-w32 2.10-pre-20160821-1, which seems to be the most recent we can update it to; still says `std::mutex does not name a type` – John Perry Aug 14 '18 at 15:30
  • Thanks @JohnPerry I will see if I there is an update to my answer based on that version. – IsakBosman Aug 14 '18 at 15:32
7

Mutex, at least, is not supported in 'Thread model: win32' of the Mingw-builds toolchains. You must select any of the toolchains with 'Thread model: posix'. After trying with several versions and revisions (both architectures i686 and x86_64) I only found support in x86_64-4.9.2-posix-seh-rt_v3-rev1 being the thread model, IMO, the determining factor.

Walter Waldo
  • 129
  • 2
  • 2
4

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.

Installed MingW version on Windows 8.1

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.

Building and running pthread application on Windows 8.1 using MingW

Parker
  • 7,244
  • 12
  • 70
  • 92
2

I got the same error with gcc4.7.7.

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

Doan Quang Viet
  • 135
  • 1
  • 5
1

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]"
Dharman
  • 30,962
  • 25
  • 85
  • 135
TungBui
  • 11
  • 2
1

my gcc version is 5.4 and i solved this problem when adding #include and also adding -std=c++11 at my CmakeLists.txt as below:

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall  -O3 -march=native -std=c++11")
Nan Shi
  • 55
  • 1
  • 1
  • 6
0

I don't know if it works for everybody, but in other way you just have to update your ndk. I'm using ndk-r11c and it works perfectly.

Bastienm
  • 363
  • 2
  • 16
  • 1
    MinGW64 has nothing to do with the _Android_ NDKs. – 0xC0000022L May 29 '19 at 10:31
  • You are right, the important point was the "11c" which is relative to the C version – Bastienm Jun 13 '19 at 15:23
  • @Bastienm The "11c" in the NDK version has nothing to do with a "C" version (and C11 is different from C++11, std::mutex is a C++ thing, not a C thing). See the [NDK Revision History](https://developer.android.com/ndk/downloads/revision_history). C++11 support (where std::mutex was added to the language) seems to have been added in Android NDK r8e (March 2013), but again this has nothing to do with MinGW. Even with C++11 (and 14, 17, ...) support in MinGW, with a non-POSIX threading model, support for std::mutex is missing. – Thomas Perl Jul 27 '22 at 08:18
-9

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.

Kolyunya
  • 5,973
  • 7
  • 46
  • 81