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

发布于 2024-11-05 11:51:32 字数 598 浏览 2 评论 0原文

当尝试编译以下代码时

#include <thread>
#include <iostream>

void foo() { std::cout << "foo\n"; }

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)

编辑: 顺便说一句,Visual Studio 2012 在此代码示例中表现良好。

When trying to compile the following code

#include <thread>
#include <iostream>

void foo() { std::cout << "foo\n"; }

int main()
{
  std::thread t(foo);
  t.join();
}

I get an error:

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

How to use C++11 experimental concurrency features? I have MinGW GCC 4.5.1 (TDM)

EDIT: BTW, Visual Studio 2012 performs good this code sample.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

Hello爱情风 2024-11-12 11:51:32

据我所知,MinGW 尚不支持新的 c++0x 并发功能(从 GCC 4.5 开始)。我记得读过一个邮件列表交换,其中指出在 MinGW 中,线程头中的以下 ifdef 不满足:

#if defined(_GLIBCXX_HAS_GTHREADS)

我想这与 Windows 下构建 MinGW 的方式有关,无论它使用本机线程还是 pthread,在我的代码中,我编写了一些最小包装,在 Windows 中使用 Boost.thread 而不是本机 c++0x 线程。这两个接口非常相似,对于许多用途来说,它们可以毫无问题地互换。

编辑:感谢 Luc Danton 挖掘出上面提到的邮件列表线程:

http://comments.gmane.org/gmane.comp.gnu.mingw.user/33065

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)

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.

EDIT: Thanks to Luc Danton for digging out the mailing list thread mentioned above:

http://comments.gmane.org/gmane.comp.gnu.mingw.user/33065

如若梦似彩虹 2024-11-12 11:51:32

我目前正在努力开发一个使用新的 mingw-w64 winpthreads 库的 GCC。这将在 GCC 中启用 posix 线程模型,并且正在努力使其正常工作。另一个 mingw-w64 用户已经通过黑客攻击(明智地)获得了此功能,但我试图在主线 GCC 中完成它,而无需在工具链安装后进行手动干预。

当前的讨论可以遵循 这里。一旦所有粗糙的边缘都被平滑掉,我将更新这个答案。

编辑:由于投票,这个答案引起了我的注意。我构建了一个实验性的 GCC 4.7,它应该与 std::thread 一起使用,但仅限于静态链接时(将 -static 添加到链接命令)。该公告位于此处< /a>.

I am currently working on getting a GCC steamed up that uses the new mingw-w64 winpthreads library. This would enable a posix threading model in GCC, and work is underway to get it working like it should. Another mingw-w64 user has already got this functioning by hacking around (sensibly), but I'm trying to get it done in mainline GCC, without manual intervention after toolchain installation.

The present discussion can be followed here. I will update this answer once all rough edges have been smoothed out.

EDIT: Due to an upvote, this answer came to my attention. I have built an experimental GCC 4.7, which should work with std::thread, but only when linking statically (add -static to the link command). The announcement is here.

北座城市 2024-11-12 11:51:32

C++0x 库状态页面 说它已经实现,但您可能需要 SVN 版本才能获取该页面上列出的所有内容。 此页面看起来可以帮助您获得最前沿的构建。

The C++0x library status page says it has been implemented, but you may need an SVN version to get everything listed on that page. This page looks like it would help you get a bleeding edge build.

伪心 2024-11-12 11:51:32

正如其他人提到的,gcc 的 mingw 端口不提供开箱即用的 C++0x 并发支持。但是,商业 just::thread 库 提供了这些设施,因此您可以使用 std::线程gcc 4.5.2的TDM/mingw端口

免责声明:我是 just::thread 库的主要开发人员。

As others have mentioned, the mingw port of gcc does not provide C++0x concurrency support out of the box. However, the commercial just::thread library provides these facilities, so you can use std::thread with the TDM/mingw port of gcc 4.5.2.

Disclaimer: I am the primary developer of the just::thread library.

稀香 2024-11-12 11:51:32

已经有 std::threads 和同步原语的轻量级本机实现:
https://github.com/meganz/mingw-std-threads

这是一个标头-only 库,它应该适用于任何 C++11 版本的 MinGW。

There is already a lightweight native implementation of std::threads and sync primitives:
https://github.com/meganz/mingw-std-threads

It is a header-only library and it should work with any C++11 version of MinGW.

风透绣罗衣 2024-11-12 11:51:32

当您获得支持 std::thread 的编译器时,这是您更正的示例(两个次要的 type-o):

#include <thread>
#include <iostream>

void foo() { std::cout << "foo\n"; }

int main()
{
  std::thread t(foo);
  t.join();
}

When you get a compiler that supports std::thread here is your corrected example (two minor type-o's):

#include <thread>
#include <iostream>

void foo() { std::cout << "foo\n"; }

int main()
{
  std::thread t(foo);
  t.join();
}
行至春深 2024-11-12 11:51:32

尝试 MinGw 构建:

http://sourceforge.net/projects/mingwbuilds/

此安装程序将允许您选择您想要的任何 MinGW,并且还包括 c++11 线程功能。

Try MinGw builds:

http://sourceforge.net/projects/mingwbuilds/

This installer will allow you to choose whatever MinGW you want and also includes c++11 thread functionality.

黎夕旧梦 2024-11-12 11:51:32

还有另一种选择。

//Threading01.cpp

#include <thread>
#include <iostream>

void hello()
{
    std::cout<< "Hello Threading ..." << std::endl;
}

int main()
{
    std::thread t1(hello);
    t1.join();

    return 0;
}

下载 mingw-w64 (sourceforge.net 上的 mingw-w64 项目正在移至 mingw-w64.org)并执行他们提供的.bat文件(mingw-w64.bat)。在提供的命令行中,你可以像这样执行你的线程代码

C:\CPP>g++ std=c++11 -g -Wall -lpthread -o Threading01 Threading01.cpp

There is an another option.

//Threading01.cpp

#include <thread>
#include <iostream>

void hello()
{
    std::cout<< "Hello Threading ..." << std::endl;
}

int main()
{
    std::thread t1(hello);
    t1.join();

    return 0;
}

Download mingw-w64 (The mingw-w64 project on sourceforge.net is moving to mingw-w64.org) and execute the .bat file(mingw-w64.bat) they provided.In the provided command line, you can execute your thread code like this

C:\CPP>g++ std=c++11 -g -Wall -lpthread -o Threading01 Threading01.cpp
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文