windows mingw-w64 线程:posix 与 win32

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17242516/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 10:37:05  来源:igfitidea点击:

mingw-w64 threads: posix vs win32

windowsgccpthreadsmingw

提问by Simon

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

我在 Windows 上安装 mingw-w64,有两个选项:win32 线程和 posix 线程。我知道 win32 线程和 pthreads 之间有什么区别,但我不明白这两个选项之间有什么区别。我怀疑如果我选择 posix 线程,它会阻止我调用像 CreateThread 这样的 WinAPI 函数。

It seems that this option specify which threading API will be used by some program or library, but by what? By GCC, libstdc++ or by something else?

似乎这个选项指定了某个程序或库将使用哪个线程 API,但是什么?通过 GCC、libstdc++ 或其他方式?

I found this: Whats the difference between thread_posixs and thread_win32 in gcc port of windows?

我发现了这个: Windows 的 gcc 端口中 thread_posixs 和 thread_win32 之间有什么区别?

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.

总之,对于这个版本的mingw,threads-posix release将使用posix API并允许使用std::thread,而threads-win32将使用win32 API,并禁用std::thread部分标准。

Ok, if I will select win32 threads then std::thread will be unavailable but win32 threads will still be used. But used by what?

好的,如果我选择 win32 线程,那么 std::thread 将不可用,但仍将使用 win32 线程。但用于什么?

采纳答案by rubenvb

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.

GCC 带有一个编译器运行时库 (libgcc),它用于(除其他外)为其支持的语言中的多线程相关功能提供低级操作系统抽象。最相关的示例是 libstdc++ 的 C++11 <thread><mutex>、 和<future>,当 GCC 使用其内部 Win32 线程模型构建时,它们没有完整的实现。MinGW-w64 提供了一个 winpthreads(在 Win32 多线程 API 之上的 pthreads 实现),然后 GCC 可以链接它以启用所有花哨的功能。

I must stress this option does not forbid you to write any code you want (it has absolutely NOinfluence 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.

我必须强调这个选项不会禁止你编写任何你想要的代码(它对你可以在代码中调用的 API绝对没有影响)。它仅反映 GCC 的运行时库 (libgcc/libstdc++/...) 用于其功能的内容。@James 引用的警告与 GCC 的内部线程模型无关,而是与 Microsoft 的 CRT 实现有关。

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.
  • posix: 启用 C++11/C11 多线程功能。使 libgcc 依赖于 libwinpthreads,这样即使您不直接调用 pthreads API,您也会分发 winpthreads DLL。为您的应用程序再分发一个 DLL 并没有错。
  • win32: 没有 C++11 多线程特性。

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

两者都不会影响调用 Win32 API 或 pthreads API 的任何用户代码。您始终可以同时使用两者。

回答by James Holderness

Parts of the GCC runtime (the exception handling, in particular) are dependent on the threading model being used. So, if you're using the version of the runtime that was built with POSIX threads, but decide to create threads in your own code with the Win32 APIs, you're likely to have problems at some point.

GCC 运行时的一部分(特别是异常处理)依赖于所使用的线程模型。因此,如果您使用的是使用 POSIX 线程构建的运行时版本,但决定使用 Win32 API 在您自己的代码中创建线程,那么您可能会在某些时候遇到问题。

Even if you're using the Win32 threading version of the runtime you probably shouldn't be calling the Win32 APIs directly. Quoting from the MinGW FAQ:

即使您使用的是运行时的 Win32 线程版本,您也不应该直接调用 Win32 API。引用MinGW 常见问题解答

As MinGW uses the standard Microsoft C runtime library which comes with Windows, you should be careful and use the correct function to generate a new thread. In particular, the CreateThreadfunction will not setup the stack correctly for the C runtime library. You should use _beginthreadexinstead, which is (almost) completely compatible with CreateThread.

由于 MinGW 使用 Windows 附带的标准 Microsoft C 运行时库,因此您应该小心并使用正确的函数来生成新线程。特别是,该CreateThread函数不会为 C 运行时库正确设置堆栈。您应该_beginthreadex改用它,它(几乎)与CreateThread.

回答by Tom 7

Note that it is now possible to use some of C++11 std::thread in the win32 threading mode. These header-only adapters worked out of the box for me: https://github.com/meganz/mingw-std-threads

请注意,现在可以在 win32 线程模式下使用某些 C++11 std::thread。这些仅标头的适配器对我来说是开箱即用的:https: //github.com/meganz/mingw-std-threads

From the revision history it looks like there is some recent attempt to make this a part of the mingw64 runtime.

从修订历史来看,最近有一些尝试将其作为 mingw64 运行时的一部分。