1

I tried a basic program:

// ThreadExample.cpp
#include <string>
#include <iostream>
#include <thread>
using namespace std;

void task1(string msg)
{
    cout << "task1 says: " << msg;
}

int main()
{
    thread t1(task1, "Hello");
    t1.join();
}

One I actually found on stackoverflow, but I tried compiling it using:

g++ -std=c++0x -pthread ThreadExample.cpp -o ThreadExample -lm

However, I keep getting an error that thread is undeclared. I have version 4.7.1 of the MinGW GNU for Windows. Is there something I can change so I can use C++11?

  • Does MinGW support posix threads? – Pubby Jan 07 '13 at 23:58
  • also try to put -pthread in the end of g++ command, order matters – billz Jan 08 '13 at 00:03
  • Yeah. Change `c++0x` to `c++11` to use C++11. However, I doubt that this has anything to do with the thread library... – leemes Jan 08 '13 at 00:06
  • It looks like it doesn't support posix threads, because Windows isn't a POSIX system: http://www.mingw.org/wiki/pthreads_library Does anyone know if this means I have to use a *nix OS, like Ubuntu to use c++11 && its thread class? I am new to c++11. –  Jan 08 '13 at 00:09
  • 1
    You could use Visual Studio. Its C++11 library implementation is mostly complete. – Benjamin Lindley Jan 08 '13 at 00:11
  • 1
    It's certainly possible to get a MinGW build that uses Pthreads, I think @rubenvb can probably give more info. Paging rubenvb ... damn, naming a user doesn't do anything if they haven't contributed to the question – Jonathan Wakely Jan 08 '13 at 00:27
  • 1
    The [builds here](http://sourceforge.net/projects/mingwbuilds/files/) have support, however, there is a lot of overhead, and I recommend using visual studio instead on windows. – Jesse Good Jan 08 '13 at 00:30
  • @JesseGood: A lot of overhead to get it working? Or a lot of overhead in the produced code? – Benjamin Lindley Jan 08 '13 at 00:36
  • 1
    @BenjaminLindley: In the produced code, at least in my tests compared to visual studio. – Jesse Good Jan 08 '13 at 00:41
  • @leemes, `-std=c++0x` and `-std=c++11` do exactly the same thing – Jonathan Wakely Jan 08 '13 at 00:47

1 Answers1

1

Noone has contributed an implementation of <thread>, <mutex> etc for Mingw yet, except when using Mingw with (optional) Pthreads support via a third-party pthreads implementation.

I started a thread at http://gcc.gnu.org/ml/libstdc++/2012-05/msg00020.html with some suggestions for implementing the missing features in terms of Windows native threads, but as I don't have a Windows machine and noone else has volunteered to do anything, nothing happened. I have almost zero interest in implementing it myself, because I never develop for Windows so it would be of no use to me whatsoever, and I would rather spend my limited free time implementing things I will actually use. If anyone wants to work on it I'd happily advise them and review their code and help shepherd it into GCC.

Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521