问题描述
当尝试编译以下代码时
#include <thread>
#include <iostream>
void foo() { std::cout << "foo
"; }
int main()
{
std::thread t(foo);
t.join();
}
我收到一个错误:
C:Test>g++ -g -Wall -lpthread -std=c++0x
main.cpp
main.cpp: In function 'int main()':
main.cpp:12:2: error: 'thread' is not a member of 'std'
main.cpp:12:14: error: expected ';' before 't'
main.cpp:13:2: error: 't' has not been declared
如何使用C++11实验性并发特性?我有 MinGW GCC 4.5.1 (TDM)
How to use C++11 experimental concurrency features? I have MinGW GCC 4.5.1 (TDM)
顺便说一句,Visual Studio 2012 在此代码示例中表现良好.
BTW, Visual Studio 2012 performs good this code sample.
推荐答案
据我所知,MinGW 尚不支持新的 c++0x 并发特性(从 GCC 4.5 开始).我记得读过一个邮件列表交换,其中指出在 MinGW 中,线程头中的以下 ifdef 不满足:
To the best of my knowledge, MinGW does not support yet the new c++0x concurrency features (as of GCC 4.5). I remember reading a mailing list exchange in which it was pointed out that in MinGW the following ifdef from the thread header is not satisfied:
#if defined(_GLIBCXX_HAS_GTHREADS)
我想这在某种程度上与 MinGW 在 Windows 下的构建方式有关,无论是使用本机线程还是 pthread 等.在我的代码中,我编写了一些使用 Boost.thread 而不是本机 c++ 的最小包装在 Windows 中为 0x 线程.这两个接口非常相似,在许多用途中它们可以毫无问题地互换.
I guess this is somehow related to the way MinGW is built under Windows, whether it uses native threads or pthread, etc. In my code, I've written some minimal wrapping that uses Boost.thread instead of native c++0x threads when in Windows. The two interfaces are very similar and for many uses they can be swapped without issues.
编辑:感谢 Luc Danton 挖掘出上述邮件列表主题:
EDIT: Thanks to Luc Danton for digging out the mailing list thread mentioned above:
http://comments.gmane.org/gmane.comp.gnu.mingw.user/33065
这篇关于如何在 MinGW 中启用实验性 C++11 并发功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!