1

Possible Duplicate:
GCC std::thread not found in namespace std

I expected this code to compile under gcc but it didnt. I'm using mingw 4.7.0. I see the header yet the classes dont seem to exist. What flags do i need? i ran it with

g++ -std=gnu++11 main.cpp

The code

#include<atomic>
#include<thread>
#include<iostream>
using namespace std;
atomic<int> a1,a2,a3;

void test(){
    cout<<"run";
}

int main(){
    thread t(test);
    t.join();
}

The error i get is thread doesn't exist. In my other code it also says std::this_thread::yield() doesnt exisit. MSVC11 compiles this fine

Community
  • 1
  • 1
  • @Tudor: thread doesnt exist. In my real code it gives me that and this_thread::yeild doesnt exist –  Sep 10 '12 at 11:50
  • Have a look at [the answer](http://stackoverflow.com/a/5931181/723845) – Loom Jan 18 '13 at 21:36

1 Answers1

6

The MingW implementation of GCC is not complete with respect to C++11. You can either 1) wait, 2) fix it yourself (open source!), or 3) use a different compiler. GCC just uses pthreads to implement the standard library threading functionality, and those aren't available on Windows.

On Linux you have to compile with g++ -std=c++11 -pthread to make the program work.


This is not the only aspect of the MingW port of GCC that's lacking. Another one is std::random_device, which tries to open /dev/urandom and predictably fails on Windows. Basically, someone needs to rewrite the guts of the implementation with the appropriate Windows API primitives, but I assume that nobody has done that yet.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • blah. This means i **must** use msvc11 for windows builds –  Sep 10 '12 at 11:55
  • @acidzombie24: Or cygwin? Or get a pthreads implementation for Windows and hack the library? – Kerrek SB Sep 10 '12 at 11:55
  • Does cygwin support it? an [old possibly outdated answer](http://stackoverflow.com/a/3417007/34537) suggest it doesnt –  Sep 10 '12 at 11:57
  • @acidzombie24: Cygwin claims to provide a Posix implementation for Win32, but I'm not sure if that includes pthreads. – Kerrek SB Sep 10 '12 at 11:58
  • I'm not using pthreads... I am using std::thread in C++11 –  Sep 10 '12 at 12:00
  • Cygwin does provide some kind of pthreads, the APIs are there. It's another matter whether the implementation is "good" enough for GCC's `std::thread` to just work, maybe so. But GCC on Cygwin doesn't produce a "proper" Windows program, it produces one that links against cygwin dlls. So for a lot of purposes it is no substitute for a Windows compiler. You don't necessarily fancy distributing the Cygwin runtime with your app, although it's an option. – Steve Jessop Sep 10 '12 at 12:00
  • 1
    @acidzombie24: As Kerrek said, GCC's implementation of `std::thread` uses pthreads. So on MSVC you aren't using pthreads, on GCC it turns out you are. – Steve Jessop Sep 10 '12 at 12:04