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?
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)
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.
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.
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).
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;
}
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?
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.
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.