問題描述
我在安裝 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++11
thread
,實際上需要在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.
這篇關于使用 Win32 線程模型時,MinGW-w64 是否支持 std::thread 開箱即用?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!