16

I downloaded the version of MinGW from the official website: http://sourceforge.net/projects/mingw/files/ and installed it on my Windows 7 machine.

Running g++ --version gives me g++.exe (GCC) 4.8.1 and I believe GCC 4.8.1 has support for C++11 features, including threads.

Running g++ -std=c++11 main.cpp successfully compiles the following program.

//main.cpp
#include <memory>

int main() {
    std::unique_ptr<int> a(new int);
    return 0;
}

But running g++ -std=c++11 main.cpp on the following program:

//main.cpp
#include <mutex>

int main() {
    std::mutex myMutex;
    return 0;
}

gives errors:

main.cpp: In function `int main()`:
main.cpp:5:5: error: 'mutex' is not a member of 'std'
    std::mutex myMutex;
    ^
main.cpp:5:16: error: expected ';' before 'myMutex'
    std::mutex myMutex;
                ^

as if <mutex> is not supported. The compiler does not complain about #include <mutex> so I have no idea why I'm getting this error.

newprogrammer
  • 2,432
  • 2
  • 24
  • 43
  • I see I don't have this problem on cygwin gcc 4.8.3. Perhaps you could take a look into your mutex header - maybe you can spot something weird in there. Unless installed somewhere else it's location should be something like this /lib/gcc/i686-pc-cygwin/4.8.1/include/c++/mutex. – dragosht Jun 03 '14 at 07:58
  • 1
    I'm using MinGW not cygwin – newprogrammer Jun 03 '14 at 08:00
  • Ok. My bad ... However taking a look into that header may be useful. After all the compiler seems not to find mutex inside the std namespace. – dragosht Jun 03 '14 at 08:03
  • Which MinGW version do you exactly use? In particular, which thread model does your version support: win32 or posix? – MWid Jun 03 '14 at 08:07
  • 1
    My MinGW has a .dll package called "mingw32-libpthreadgc" or "POSIX threading library for Win32". But this is just the runtime library, so I just installed a package called mingw32-pthreads-w32, a "POSIX threading library for Win32", which contains the headers and development files. I thought that would fix it, but I'm still getting the same error. – newprogrammer Jun 03 '14 at 08:19
  • You can try the following build [mingw-build](http://sourceforge.net/projects/mingw-w64/files/latest/download?source=files). Choose the posix threading model. – MWid Jun 03 '14 at 08:42
  • See also https://stackoverflow.com/questions/17242516/mingw-w64-threads-posix-vs-win32 which is the state of the art for mingw-w64, no idea on mingw main... – rogerdpack Oct 25 '18 at 04:39

2 Answers2

10

If I understand well, std threading is still not supported on mingw, but some mingw-w64 builds support it. Fortunately, you can still build 32-bit apps using this version of mingw.

Here is the link for the builds.

slaadvak
  • 3,921
  • 1
  • 20
  • 32
  • 3
    To clarify: using a pthread build (either pre-compiled, or if you select pthread from the mingw-w64 installer) works out-of-the box; using a win32-thread build you need to use the meganz addon as described in Alexander Vassilev's answer. – M.M Jun 22 '15 at 23:32
9

There is already a native win32 implementation of std::thread and sync primitives: https://github.com/meganz/mingw-std-threads It is a header-only library and should work with any C++11 compliant version of MinGW.

Alexander Vassilev
  • 1,310
  • 13
  • 20