問題描述
我在安裝 MinGW-w64 工具鏈時(shí)選擇了 Win32 線程模型,在閱讀它之后,它提供了比 POSIX 對(duì)應(yīng)的更好的性能.我自己沒有資格對(duì)此聲明進(jìn)行基準(zhǔn)測(cè)試,但是 這里是它的來源.
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.
起初我認(rèn)為這個(gè)選項(xiàng)只會(huì)影響 GCC 運(yùn)行時(shí)的內(nèi)部工作,而不會(huì)阻止我在我的代碼中使用 C++11 線程,基于 此答案 和此評(píng)論 來自其他用戶 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.
我正在從命令行調(diào)用 g++,除了 -std=c++11
之外沒有其他選項(xiàng).
I am invoking g++ from the command line with no options other than -std=c++11
.
此時(shí)我不確定:
- rubenvb 搞錯(cuò)了,為了編譯我的代碼依賴于C++11
thread
,實(shí)際上需要在POSIX線程模型下安裝MinGW-w64圖書館,或; - 我完全誤解了這一切,或者;
std::thread
實(shí)際上在我的場(chǎng)景中是支持的,只是不直觀.
- 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.
我加強(qiáng)了標(biāo)題中的開箱即用"部分.存在一個(gè)名為 mingw-std-threads 的庫(kù),如 這個(gè)答案.但是,作為第三方選項(xiàng),與此問題無關(guān).
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 月),當(dāng)與 Win32 內(nèi)部線程模型一起安裝時(shí),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 本機(jī)線程一起使用,您可以安裝 mingw-std-threads 頭.
To use the MinGW-w64 with Win32 native threads you can install the mingw-std-threads headers.
如該頁面所述,這是因?yàn)?MinGW-w64 是 GCC 的一個(gè)端口,但 GCC 不包含任何本機(jī)線程支持.相反,GCC 安裝通常通過 gthreads 或 pthreads 作為 glibc 的一部分來實(shí)現(xiàn)線程.MinGW-w64 不包含 glibc 的端口.(相反,它結(jié)合使用了 MSVC 運(yùn)行時(shí)加上自己的代碼來填補(bǔ)漏洞).
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 的最新版本確實(shí)包含 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.
這篇關(guān)于使用 Win32 線程模型時(shí),MinGW-w64 是否支持 std::thread 開箱即用?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!