0

Attempting to use threads in C++ for the first time. Here is my code:

#include <iostream>
#include <thread>

int main(){
  std::thread th;
}

To compile, I am using:

g++ -g -Wall -std=c++11 -pthread threadtest.cpp

This gives the following error:

$ g++ -g -Wall -std=c++11 -pthread threadtest.cpp
threadtest.cpp: In function 'int main()':
threadtest.cpp:5:8: error: 'thread' is not a member of 'std'
    5 |   std::thread th;
      |        ^~~~~~
threadtest.cpp:3:1: note: 'std::thread' is defined in header '<thread>'; did you forget to 
'#include <thread>'?
    2 | #include <thread>
  +++ |+#include <thread>
    3 |

If I comment out the "std::thread th" line, the error message changes to:

$ g++ -g -Wall -std=c++11 -pthread threadtest.cpp
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: cannot find -lpthread
collect2.exe: error: ld returned 1 exit status

Operating system: Windows 10. Seems to get the same result whether I'm using the plane-old windows command line or if I do it in Cygwin. Anyone know what's wrong? Thanks in advance.

Danny
  • 161
  • 1
  • 1
  • 3
  • 3
    There are different variations of MinGW, some of them suck more than the others. You got the one that doesn't support threads (the original MinGW, as opposed to MinGW-w64). Get MinGW-w64 from [MSYS2](https://stackoverflow.com/q/30069830/2752075). – HolyBlackCat Apr 16 '22 at 20:27

0 Answers0