使用 Win32 线程模型时,MinGW-w64 是否支持 std::thread 开箱即用?

2021-12-06 00:00:00 gcc multithreading mingw-w64 c++ c++11

我在安装 MinGW-w64 工具链时选择了 Win32 线程模型,在阅读它之后,它提供了比 POSIX 对应的更好的性能.我自己没有资格对此声明进行基准测试,但是 这里是它的来源.

I have opted for the Win32 threading model when installing the MinGW-w64 toolchain, after reading that it provides better performance than the POSIX counterpart. I am not qualified for benchmarking this claim myself, but here's a source for it.

起初我认为这个选项只会影响 GCC 运行时的内部工作,而不会阻止我在我的代码中使用 C++11 线程,基于 此答案 和此评论 来自其他用户 rubenvb.
然而,情况似乎并非如此.std::thread 在此 MinGW-w64 安装中似乎不存在支持.

At first I thought this option would only affect the inner workings of the GCC runtime, while not preventing me from using C++11 threads in my code, based on this answer and this comment by fellow user rubenvb.
However, this doesn't seem to be the case. std::thread support appears to be non-existent in this MinGW-w64 installation.

我正在从命令行调用 g++,除了 -std=c++11 之外没有其他选项.

I am invoking g++ from the command line with no options other than -std=c++11.

此时我不确定:

  • rubenvb 搞错了,为了编译我的代码依赖于C++11thread,实际上需要在POSIX线程模型下安装MinGW-w64图书馆,或;
  • 我完全误解了这一切,或者;
  • std::thread 实际上在我的场景中是支持的,只是不直观.
  • rubenvb was mistaken, and it's actually necessary to install MinGW-w64 under the POSIX threading model in order to compile my code which depends on the C++11 thread library, or;
  • I completely misunderstood it all, or;
  • std::thread is actually supported in my scenario, it's just not intuitive.

我加强了标题中的开箱即用"部分.存在一个名为 mingw-std-threads 的库,如 这个答案.但是,作为第三方选项,与此问题无关.

I reinforce the "out of the box" part in the title. There exists a library called mingw-std-threads, as presented in this answer. However, as a third-party option, it is not relevant to this question.

那么,截至今天(2016 年 5 月),当与 Win32 内部线程模型一起安装时,MinGW-w64 是否本身支持 std::thread 依赖代码?

So, as of today (May 2016), does MinGW-w64 nativelly support std::thread depending code, when installed with the Win32 internal threading model?

推荐答案

要将 MinGW-w64 与 Win32 本机线程一起使用,您可以安装 mingw-std-threads 头.

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

如该页面所述,这是因为 MinGW-w64 是 GCC 的一个端口,但 GCC 不包含任何本机线程支持.相反,GCC 安装通常通过 gthreads 或 pthreads 作为 glibc 的一部分来实现线程.MinGW-w64 不包含 glibc 的端口.(相反,它结合使用了 MSVC 运行时加上自己的代码来填补漏洞).

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).

同样如该页面所述,MinGW-w64 的最新版本确实包含 pthreads 的 Win32 端口(winpthreads"),这解释了为什么您可以通过选择pthread"模型让线程开箱即用"工作来自 MinGW-w64 安装程序.

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.

相关文章