Question:
How could I compile c++ code (mingw) referencing c++ thread?
?
2013-08-01 13:48:58 UTC
I would like to compile the following code:
http://www.cplusplus.com/reference/thread/thread/?kw=thread
I read the article multithreading = C++ 2011 on stackoverflow.
http://stackoverflow.com/questions/10792157/c-2011-stdthread-simple-example-to-parallelize-a-loop
This is what i tried as a command line:
g++ -std=c++11 source.cpp
this is the message I received:
t.cpp: In function 'int main()':
t.cpp:18:3: error: 'thread' was not declared in this scope
thread first (foo); // spawn new thread that calls foo()
^
t.cpp:18:10: error: expected ';' before 'first'
thread first (foo); // spawn new thread that calls foo()
^
t.cpp:19:10: error: expected ';' before 'second'
thread second (bar,0); // spawn new thread that calls bar(0)
^
t.cpp:24:3: error: 'first' was not declared in this scope
first.join(); // pauses until first finishes
^
t.cpp:25:3: error: 'second' was not declared in this scope
second.join(); // pauses until second finishes
^
This pertains to above code you can find here:
http://www.cplusplus.com/reference/thread/thread/?kw=thread
In this directory there *is* an include file named thread
C:\mingw\lib\gcc\mingw32\4.7.2\include\c++
The same as above if I use this compiler:
C:\Program Files\mingw-builds\x32-4.8.1-posix-dwarf-rev2
C:\Program Files\mingw-builds\x32-4.8.1-posix-dwarf-rev2\mingw32\lib\gcc\i686-w64-mingw32\4.8.1\include\c++

Actually on ideone (c++11) above code seems to compile and link, just has some runtime issues ...
http://ideone.com/qbrqO1

Please let me know, what I can do.
Three answers:
?
2013-08-02 18:50:15 UTC
Some gcc versions do not support std::thread. You'll have to upgrade your compiler to a newer one or use a different compiler. Clang compiles the code perfectly however.
_Object
2013-08-01 19:47:46 UTC
Make sure to link with -pthread. The code is valid; you just have your compiler configuration wrong.
?
2014-06-18 21:52:25 UTC
Try this link in case you are interested in a windows compiler:

http://tdm-gcc.tdragon.net/download

or here

http://sourceforge.net/projects/tdm-gcc/


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...