如何在 MinGW 中启用实验性 C++11 并发功能?

How to enable experimental C++11 concurrency features in MinGW?(如何在 MinGW 中启用实验性 C++11 并发功能?)
本文介绍了如何在 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 并发功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Prevent class inheritance in C++(防止 C++ 中的类继承)
Why should I declare a virtual destructor for an abstract class in C++?(为什么要在 C++ 中为抽象类声明虚拟析构函数?)
Why is Default constructor called in virtual inheritance?(为什么在虚拟继承中调用默认构造函数?)
C++ cast to derived class(C++ 转换为派生类)
C++ virtual function return type(C++虚函数返回类型)
Is there any real risk to deriving from the C++ STL containers?(从 C++ STL 容器派生是否有任何真正的风险?)