0

I am trying to learn C++, and I cant seem to get threading to work.

#include <iostream>  
#include <thread>  
using namespace std;  

void thing(){  
    cout << "1 is a thing" << endl;  
    int num;
    cin >> num;
}  

int main(){  
    cout << "is 1 a thing?" << endl;  
    thread neat1(thing);  
    return 0;
}  

identifier "thread" is undefined
Im on C++14

minGW info:
Using built-in specs. COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=c:/mingw/bin/../libexec/gcc/mingw32/8.2.0/lto-wrapper.exe
Target: mingw32
Configured with: ../src/gcc-8.2.0/configure --build=x86_64-pc-linux-gnu --host=mingw32 --target=mingw32 --prefix=/mingw --disable-win32-registry --with-arch=i586 --with-tune=generic --enable-languages=c,c++,objc,obj- c++,fortran,ada --with-pkgversion='MinGW.org GCC-8.2.0-3' --with-gmp=/mingw --with-mpfr=/mingw --with-mpc=/mingw --enable-static --enable-shared --enable-threads --with-dwarf2 --disable-sjlj-exceptions --enable-version- specific-runtime-libs --with-libiconv-prefix=/mingw --with-libintl- prefix=/mingw --enable-libstdcxx-debug --with-isl=/mingw --enable-libgomp --disable-libvtv --enable-nls --disable-build-format-warnings
Thread model: win32
gcc version 8.2.0 (MinGW.org GCC-8.2.0-3)

this is what happends if I compile via cmd

>g++ first.cpp -std=c++11  
first.cpp: In function 'int main()':  
first.cpp:13:5: error: 'thread' was not declared in this scope  
     thread neat1(thing);  
     ^~~~~~  
first.cpp:13:5: note: 'std::thread' is defined in header '<thread>'; did you   forget to '#include <thread>'?  
first.cpp:3:1:  
+#include <thread>  
 using namespace std;  
first.cpp:13:5:  
     thread neat1(thing);  
     ^~~~~~
yes
  • 1
  • 1
  • 3
  • Check if you're using at least C++11. – João Paulo Sep 12 '19 at 17:16
  • 4
    1) `main(){` should be `int main(){` 2) Are you compiling with C++-11 enabled? – Algirdas Preidžius Sep 12 '19 at 17:17
  • What is your compiler version and how do you compile (options used for compilation)? – Yksisarvinen Sep 12 '19 at 17:17
  • Im using minGW and by options do you mean the .json's? (sorry as you can tell, I'm very new to alot of this) – yes Sep 12 '19 at 17:24
  • What version? I don't think the 32 bit version supports `thread` – NathanOliver Sep 12 '19 at 17:27
  • Where did you get your MinGW? What does `g++ --version` report? – HolyBlackCat Sep 12 '19 at 17:30
  • I assume having minggw32 in my MinGW folder means Im running 32bit MinGW also "g++ --version" reports g++ (MinGW.org GCC-8.2.0-3) 8.2.0 – yes Sep 12 '19 at 17:33
  • @yes they mean the compiler command line. The command line might be generated by these jsons you speak of, but it'll probably be easiest to cut-n-paste the command line from the build output. Where is the build output? Depends on the tools, unfortunately. – user4581301 Sep 12 '19 at 17:34
  • your messing with .json files suggests you may be using Visual Studio Code. Since you're a newcomer to C++, you might want to consider a development environment that's easier to set up out of the box like one of the community editions of Visual Studio. Once you have a better feel for the lay of the land you can get back to VSC or other tools you want to try out.. – user4581301 Sep 12 '19 at 17:37
  • "g++ --version" reports "g++ (MinGW.org GCC-8.2.0-3) 8.2.0" also yes I am using Visual Studio Code but did get it to compile most programs – yes Sep 12 '19 at 17:37
  • Huh. My bad. The --version output is somewhat truncated and leaves out the target. Give `g++ -v` a go. What you are looking for is about 4-5 lines down and should start with *Target*. Mine says : *Target: x86_64-w64-mingw32* and is 64 bit. The *mingw32* on the end doesn't mean much, unfortunately, so paste the whole line. – user4581301 Sep 12 '19 at 17:43
  • I put the g++ -v resault in the post – yes Sep 12 '19 at 17:50
  • Quit the IDE, enter the terminal (console) by pressing Win+R and entering "cmd", then compile the program with `g++ -std=c++11`. What is the result? – L. F. Sep 13 '19 at 01:35
  • I compiled through cmd and put the resault in the post – yes Sep 13 '19 at 05:29
  • Possible duplicate of [Does MinGW-w64 support std::thread out of the box when using the Win32 threading model?](https://stackoverflow.com/questions/37358856/does-mingw-w64-support-stdthread-out-of-the-box-when-using-the-win32-threading) – walnut Sep 13 '19 at 08:01
  • Thanks for all the help! – yes Sep 13 '19 at 17:40

1 Answers1

0

My compiler turned out to be the issue, so I removed the old one and got a new MinGW compiler from https://sourceforge.net/projects/mingw-w64/files/ from the online installer. After setting that up threading worked.

yes
  • 1
  • 1
  • 3