Does Mingw-W64 Support Std::Thread Out of the Box When Using the Win32 Threading Model

Does MinGW-w64 support std::thread out of the box when using the Win32 threading model?

To use the MinGW-w64 with Win32 native threads you can install the mingw-std-threads headers.

As described on that page, this is because MinGW-w64 is a port of GCC, but GCC does not include any native thread support. Instead GCC installations typically implement threading via either gthreads or pthreads as a part of glibc. MinGW-w64 does not include a port of glibc. (Instead it uses a combination of the MSVC runtime, plus its own code to fill in holes).

Also as described on that page, recent versions of MinGW-w64 do include a Win32 port of pthreads ("winpthreads"), which explains why you can have threads work "out of the box" by selecting the "pthread" model from the MinGW-w64 installer.

mingw-w64 threads: posix vs win32

GCC comes with a compiler runtime library (libgcc) which it uses for (among other things) providing a low-level OS abstraction for multithreading related functionality in the languages it supports. The most relevant example is libstdc++'s C++11 <thread>, <mutex>, and <future>, which do not have a complete implementation when GCC is built with its internal Win32 threading model. MinGW-w64 provides a winpthreads (a pthreads implementation on top of the Win32 multithreading API) which GCC can then link in to enable all the fancy features.

I must stress this option does not forbid you to write any code you want (it has absolutely NO influence on what API you can call in your code). It only reflects what GCC's runtime libraries (libgcc/libstdc++/...) use for their functionality. The caveat quoted by @James has nothing to do with GCC's internal threading model, but rather with Microsoft's CRT implementation.

To summarize:

  • posix: enable C++11/C11 multithreading features. Makes libgcc depend on libwinpthreads, so that even if you don't directly call pthreads API, you'll be distributing the winpthreads DLL. There's nothing wrong with distributing one more DLL with your application.
  • win32: No C++11 multithreading features.

Neither have influence on any user code calling Win32 APIs or pthreads APIs. You can always use both.

thread' is not a member of 'std' in Eclipse Neon with MinGW 6.3 GCC on Windows

With the advice of sbabbi, "Mingw32 does not support std::thread. ...You can switch to mingw64+winpthread (but TLS is utterly broken in that config) or use Boost.thread.", I decided to set up Boost and use that instead. I also switched to cygwin because it has the Boost libraries built in and I switched to Netbeans because installing Boost in eclipse was proving itself to be unnecessarily difficult.

What's the difference between thread_posixs and thread_win32 in gcc port of Windows?

So, the link you provided leads to builds of the standalone gcc 4.7.2 for windows, a.k.a mingw64. In order to build this compiler, a set of scripts are used, which help define the options of compilations. The scripts are simply called MinGW-builds, and can be found in different places:

  • google code
  • github fork

The scripts have an option which specify which thread model is to be used for the std::threads part of the C++11 standard (this is allowed for MinGW thanks to an experimental patch applied on that version of GCC).
In one case, the win32 thread API is used, and in the other case it's the posix API which is used.

Note that Windows doesn't support all the POSIX API out of the box, so some external emulation library needs to be used (winpthreads).

The GCC source configure script has an option to specify that API (--enable-threads=), and that's what is used in the build scripts.

In short, for this version of mingw, the threads-posix release will use the posix API and allow the use of std::thread, and the threads-win32 will use the win32 API, and disable the std::thread part of the standard.

Meaning of options in mingw-w64 installer

Exceptions

Please see this answer for all three models (dwarf, sjlj and seh).

Threads

You can decide what kind of threads you want to use: POSIX threads or Windows API threads. The posix threads have the advantage of portability; you can use your code on other posix platforms (eg. linux) without modifications. The win32 threading api is windows only. If you are 100% on windows and like it's api that's no problem though.

If you use new C++ features like std::thread the impact is less visible since you already have a standard api for threading. I'm not sure if there's really a big difference if you don't use posix- / win32 thread api directly (maybe std::thread native handles?)

See also: mingw-w64 threads: posix vs win32

Build revision

I guess that's just another version number since Mingw(-w64) follows GCC versions (4.8.x, 4.9.x etc.). If you don't need an specific build, you should use the latest version.

Threading issue

If the exception thrown is:

terminate called after throwing an instance of 'std::system_error'
what(): Enable multithreading to use std::thread: Operation not permitted

then just link pthreads - and the problem is solved.


Recommendation

If you don't have reasons to use a specific option; my personal recommendation:

posix - dwarf - 2
  • Posix enable C++11 <thread>, <mutex> and <future>
  • dwarf is faster
  • 2 because it's the latest release


Related Topics



Leave a reply



Submit