Mingw-w64 threads: posix vs win32

I install mingw-w64 on Windows, and there are two options: win32 threads and posix threads. I know what the difference is between win32 threads and pthreads, but I don't understand what is the difference between these two options. I doubt that if I select posix threads, this will prevent me from invoking WinAPI functions like CreateThread.

This parameter seems to indicate which streaming API will be used by any program or library, but what? By GCC, libstdc ++, or something else?

I found this: What is the difference between thread_posixs and thread_win32 in windows gcc port?

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

Well, if I choose win32 threads, then std :: thread will not be available, but win32 threads will still be used. But is that used?

+65
gcc windows pthreads mingw
Jun 21 '13 at 19:01
source share
3 answers

GCC comes with a compiler runtime library (libgcc), which it uses to (among other things) for low-level abstraction of the OS for multithreading related functions in the languages ​​it supports. The most important example is libstdC ++ C ++ 11 <thread> , <mutex> and <future> , which do not have full implementation when GCC is built with the internal Win32 streaming model. MinGW-w64 provides winpthreads (an implementation of pthreads on top of the Win32 multithreading API) that GCC can then plug in to enable all the fancy features.

I must emphasize that this option does not forbid you to write any code that you want (it has absolutely NO influence on which API you can call in your code). It reflects only those GCC runtime libraries (libgcc / libstdC ++ / ...) for their functionality. The disclaimer quoted by @James has nothing to do with the GCC internal stream model, but rather with the Microsoft CRT implementation.

Summarizing:

  • posix : enable multithreading C ++ 11 / C11. Makes libgcc depend on libwinpthreads, so even if you don’t use the pthreads API directly, you will distribute winpthreads DLLs. There is nothing wrong with distributing another DLL with your application.
  • win32 : No multithreaded C ++ 11 functions.

None of these affect any user code that calls the Win32 or pthreads APIs. You can always use both.

+55
May 22 '15 at 7:00
source share

Parts of the GCC runtime (in particular exception handling) depend on the streaming model used. So, if you are using a version of the runtime that was built with POSIX threads, but decided to create threads in your own code using the Win32 API, you might have problems at some point.

Even if you use a streaming version of Win32 for execution, you probably should not directly access the Win32 API. Quoting from MinGW Frequently Asked Questions :

Since MinGW uses the standard Microsoft C runtime library that ships with Windows, you have to be careful and use the right function to generate a new thread. In particular, the CreateThread function will not properly install the stack for the C runtime library. Instead, you should use _beginthreadex , which is (almost) fully compatible with CreateThread .

+14
Jun 22 '13 at 19:13
source share

Note that you can now use some of the std :: C ++ 11 streams in win32 streaming mode. These header-only adapters were clear to me: https://github.com/meganz/mingw-std-threads

From the revision history, there seems to be a recent attempt to make this part of the mingw64 runtime.

+7
Sep 19 '15 at 15:32
source share



All Articles