MinGW and std::thread

Lukas picture Lukas · Mar 30, 2013 · Viewed 11.9k times · Source

So I've been trying to get the following code to compile and run on Windows by using a MinGW compiler.

#include <iostream>
#include <thread>

void test()
{
    std::cout << "test" << std::endl;
}

int main()
{
    std::thread t(test);
}

I'm compiling with the following command:

g++ -std=c++11 test.cpp -o test.exe

Now the problem is the version of MinGW one should use and I've tried about all the versions I know of.

  1. MinGW-builds: thread-win32
  2. MinGW-builds: thread-posix
  3. MinGW-w64: stdthread experimental rubenvb
  4. MinGW-w64: stdthread experimental rubenvb 4.7

Number 1 doesn't work, since GCC apparently only supports pthread stuff internally.

Number 2 does compile and it essentially even outputs test (see the last line of the output), but it also crashes with the error:

terminate called without an active exception

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
test

Number 3 and 4 again do compile, but they don't output test and instead instantly crashes, but with a more descriptive output:

terminate called after throwing an instance of 'std::system_error'
  what():  Enable multithreading to use std::thread: Operation not permitted

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

Google brought me of course to the GCC bug tracker and some other posts, that suggested to use -pthread, which doesn't help at all.

I've also tried manually linking against winpthread and pthread, but that doesn't do anything either.

There's also no difference between -std=c++11 and -std=gnu++11...

I'm really lost right now and don't know, if it's at all possible to get a MinGW version, that supports std::thread, but maybe I'm just overlooking some compiler flags. I hope someone out there can help me!

Answer

Kerrek SB picture Kerrek SB · Mar 30, 2013

You forgot to join your thread:

t.join();