22

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.

Loom
  • 9,768
  • 22
  • 60
  • 112
  • See this [answer](http://stackoverflow.com/questions/2519607/c0x-stdthread-error-thread-not-member-of-std/2520980#2520980). – Bryan Drewery May 08 '11 at 22:39

8 Answers8

14

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

bluescarni
  • 3,937
  • 1
  • 22
  • 33
  • 2
    The exchange in question: http://comments.gmane.org/gmane.comp.gnu.mingw.user/33065 – Luc Danton May 09 '11 at 04:00
  • @Luc: thanks for the exchange, it makes sense that they would have portability issues here. – Matthieu M. May 09 '11 at 07:08
  • 3
    FYI: the dtors of std::thread and boost::thread have quite different semantics for threads which are still joinable – smerlin May 09 '11 at 14:26
  • that link is dead and Alexander's [answer](http://stackoverflow.com/a/27422046/1040495) (below) points to a really lightweight wrapping header – user1040495 Sep 04 '16 at 21:10
5

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.

rubenvb
  • 74,642
  • 33
  • 187
  • 332
4

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.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
4

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.

Anthony Williams
  • 66,628
  • 14
  • 133
  • 155
3

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.

Alexander Vassilev
  • 1,399
  • 13
  • 23
2

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();
}
Howard Hinnant
  • 206,506
  • 52
  • 449
  • 577
1

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.

1

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
POQDavid
  • 195
  • 1
  • 15