MinGW + Threads

Operating System: Windows XP SP3
Compiler: MinGW g++ 4.7.2
Code:
1
2
3
4
5
6
7
8
9
#include <thread>

void g() {}

int main() {
   std::thread t(g);
   
   return 0;
}

Commands:
mingw32-g++.exe -Wall  -O2  -std=c++11    -c "C:\Programming\Thread Practice\main.cpp" -o obj\Release\main.o

Errors:
C:\Programming\Thread Practice\main.cpp||In function 'int main()':|
C:\Programming\Thread Practice\main.cpp|6|error: 'thread' is not a member of 'std'|
C:\Programming\Thread Practice\main.cpp|6|error: expected ';' before 't'|
||=== Build finished: 2 errors, 0 warnings (0 minutes, 2 seconds) ===|


After doing some research, apparently MinGW still doesn't have a port for the threads library for Windows. Has anyone gotten this to work yet?
Use Boost library instead, it has almost the same syntax.
I'm aware of the boost libraries, and I have already used boost::thread as well. However, I recently deleted all of my boost stuff and am trying to stick with the standard. I deal with a lot of hassles, one less compiler command or library I need to include would be a god send. I also don't want to go through the process of putting boost back on my system (I'm running low on HDD space) just for threads (I know there is a bunch of other stuff, but most has been implemented in C++11, and the rest I don't foresee myself using anytime in the near future).
Then you either use another compiler (maybe VS2012 supports c++ 11 threads - but if you run XP you cannot use it) or you do not use C++11 features at all.

MinGW for example does not even fully supports Unicode these days (just an example)
Last edited on
I can't even install VS2012 on my computer due to hardware limitations (MS's excuse, not mine).

I've been able to use every other C++11 feature that I've tried so far (I have numerous examples on this forum). I'm just saddened that MinGW hasn't implemented threads for Windows yet.

I was not aware it didn't fully support Unicode yet, but like what specifically? I don't use Unicode nearly at all, but it would be nice to know what's actually limited.

Maybe once I free up some more space, I'll put boost back on my computer, or I'll switch to my Linux partition and just practice threads there. I just like a one place stop to do everything I need. Are you aware if the Cygwin port uses threads?
Nothing is stopping you to use _beginthreadex() from process.h, it is available in MinGW too and is not a C++11 feature.

MinGW support Unicode if you use -municode switch (only recent versions) for wWinMain (GUI applications), but even then there is no support for console applications built for Unicode (wmain entry point). You will get a linker error if you try to do that.
Last edited on
I hate using C, but there is no alternative for Windows at the moment. I also hate the WinAPI. So far, it looks like process.h requires both. Is this true concurrency? Looking at some examples (takes me a while to completely understand what's going on since I'm not familiar with C), I'm trying to follow how it works.

So far I'm understanding that beginthread returns a handle to said thread, and endthread needs to be written into the function that is going to be a thread. This is a little different than what I was thinking. I'm reading the information from here http://www.digitalmars.com/rtl/process.html but I'm kind of confused. What's the difference between _beginthread and _spawn? spawn looks like it might be the better choice, but maybe I missed something when reviewing it. I also didn't see _beginthreadex() anywhere on that page. Maybe it's too old?

I'm also assuming that I'll have to wrap any variables into their own thread safe class to prevent them from being accessed while being used. I'm just trying to get a grasp of this before I jump head first into coding since C isn't a strong point for me at all.

Edit: Did some digging on SO, and found more information between _beginthread() and _beginthreadex(). I am most definitely using the later, however, I need to research the "security" parameter. Ugh.
Last edited on
MinGW has been having std::thread support since 4.7 (although if it turned out that the version from www.mingw.org is an exception to this, I wouldn't be surprised).

http://nuwen.net/mingw.html
or: http://tdm-gcc.tdragon.net/download

Edit: as noted, those editions don't have std::thread support after all.
Last edited on
tdm doesn't seem to have std::thread support
C:\Users\Sean\Downloads>mingw32-g++ -v
Using built-in specs.
COLLECT_GCC=mingw32-g++
COLLECT_LTO_WRAPPER=c:/mingw32/bin/../libexec/gcc/mingw32/4.7.1/lto-wrapper.exe
Target: mingw32
Configured with: ../../src/gcc-4.7.1/configure --build=mingw32 --enable-language
s=c,c++,ada,fortran,objc,obj-c++ --enable-threads=win32 --enable-libgomp --enabl
e-lto --enable-fully-dynamic-string --enable-libstdcxx-debug --enable-version-sp
ecific-runtime-libs --with-gnu-ld --disable-nls --disable-win32-registry --disab
le-symvers --disable-build-poststage1-with-cxx --disable-werror --prefix=/mingw3
2tdm --with-local-prefix=/mingw32tdm --enable-cxx-flags='-fno-function-sections
-fno-data-sections' --with-pkgversion=tdm-1 --enable-sjlj-exceptions --with-bugu
rl=http://tdm-gcc.tdragon.net/bugs
Thread model: win32
gcc version 4.7.1 (tdm-1)

Same with nugen
C:\MinGW>g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=c:/mingw/bin/../libexec/gcc/i686-pc-mingw32/4.7.2/lto-wrappe
r.exe
Target: i686-pc-mingw32
Configured with: ../src/configure --prefix=/c/temp/gcc/dest --with-gmp=/c/temp/g
cc/gmp --with-mpfr=/c/temp/gcc/mpfr --with-mpc=/c/temp/gcc/mpc --enable-language
s=c,c++ --with-arch=i686 --with-tune=generic --disable-libstdcxx-pch --disable-n
ls --disable-shared --disable-sjlj-exceptions --disable-win32-registry --enable-
checking=release --enable-lto
Thread model: win32
gcc version 4.7.2 (GCC)


They both give this error

C:\Users\Sean\Downloads>g++ thread.cpp
thread.cpp: In function 'int main()':
thread.cpp:6:2: error: 'thread' is not a member of 'std'
thread.cpp:6:14: error: expected ';' before 't'
thread.cpp:6:55: error: expected primary-expression before ')' token
thread.cpp:6:55: error: expected ';' before ')' token


With this code
1
2
3
4
5
6
7
8
#include <iostream>
#include <thread>

int main()
{
	std::thread t([](){std::cout<<"hello from thread 2";});
	std::cout<<"hello from thread 1"<<std::endl;
}
Last edited on
</3 <--- My hopes of using C++11 Threads.

Sigh, this isn't going to be easy, but it'll be one more thing, like STL list, that I can add to my arsenal of code. I'm starting to understand MSDN on this one (first article I might have ever understood in it's entirety)...wait...does that mean I'm actually learning to think properly?
You can do what I do and simply swap out std::thread with boost::thread. The APIs for each are 100% identical as far as I've been able to tell (I've been using my C++11 reference when writing boost code and it hasn't failed me yet)

You can also hide the namespace you're using so that it can be easily swapped out if/when you use a compiler that supports std::thread.

1
2
3
namespace thd = boost;  // or = std if std::thread is supported

thd::thread  yourthread(...);
Sounds like a plan. I'm just so disappointed in MinGW about this whole thing. It supports threads, uses Win32 threading, but it doesn't exist to be used...

I also believe I'm going to be taking a break from programming to work on learning some more of my scripting language. My scripts are in high demand all of a sudden.
I know I've successfully compiled some projects using std::thread for Windows before. I recall that it was indeed not TDM, but a 4.7 nightly. Either it had special patches or std::thread (or rather, gthread) support was removed before the final release. Either way, it's frustrating.

Besides using boost as a drop-in, these patches might be worth a try:
http://tehsausage.com/mingw-std-thread-gcc-4-7
The advantage is that they wouldn't require building MinGW yourself.
I tried the patches and using the same code as above (and used the suggested libraries) I get this:
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.

Process returned 3 (0x3)   execution time : 0.172 s
Press any key to continue.


Now, it's very possible I did something wrong, but from reading it, it seems pretty straight forward. I'm wondering if it's not compatible with 4.7.2. What version did you use? 4.7.0?

There was also a note about MinGW64 threads being more compatible, but I have a 32bit system. I won't be able to use a 64 bit compiler, will I?
That's expected, you must join() or detach() the thread before it is destructed. See
http://en.cppreference.com/w/cpp/thread/thread/~thread
I'm also having std::thread problems with gcc-4.7.2 in linux. This code compiles and runs with boost but not std thread.
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <thread>
//#include <boost/thread.hpp>

namespace thrd = std;

int main()
{
	thrd::thread t([](){std::cout<<"thread2"<<std::endl;});
	std::cout<<"thread1"<<std::endl;
	t.join();
}
Using std::thread gives this output.
sean@sean-virtual-machine:~/Desktop$ g++ --std=c++0x thread.cpp
sean@sean-virtual-machine:~/Desktop$ ./a.out 
terminate called after throwing an instance of 'std::system_error'
  what():  Operation not permitted
Aborted (core dumped)


So it looks like for now, for me at least, it's VS2012 or boost for threading.
Last edited on
It compiles fine with MinGW, but I'm not sure if you need to link to a library on Linux or not. Maybe someone here can enlighten us.
on Linux you get operation not permitted from gcc when you forget -pthread.
Last edited on
Thank you Cubbi, I didn't realize that was necessary for std::thread.
It's just a gcc quirk. Intel requires -pthread just like gcc, but produces a more meaningful message ("Enable multithreading to use std::thread), and Clang++ works without any special switches.
Topic archived. No new replies allowed.