How to enable experimental C++11 concurrency features in MinGW?(如何在 MinGW 中启用实验性 C++11 并发功能?)
问题描述
当尝试编译以下代码时
#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 并发功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 MinGW 中启用实验性 C++11 并发功能?
基础教程推荐
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- CString 到 char* 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
