0

I am getting this error when trying to compile my code.

C:\Users\Quadsam\Desktop\Thing>g++ main.cpp -o main.exe
main.cpp: In function 'int main()':
main.cpp:13:10: error: 'thread' is not a member of 'std'
   13 |     std::thread threadObj(thread_function);
      |          ^~~~~~
main.cpp:3:1: note: 'std::thread' is defined in header '<thread>'; did you forget to '#include <thread>'?
    2 | #include <thread>
  +++ |+#include <thread>
    3 |
main.cpp:16:5: error: 'threadObj' was not declared in this scope
   16 |     threadObj.join();
      |     ^~~~~~~~~

this is my code main.cpp

#include <iostream>
#include <thread>

void thread_function()
{
    for(int i = 0; i < 10000; i++);
        std::cout<<"thread function Executing"<<std::endl;
}

int main()  
{

    std::thread threadObj(thread_function);
    for(int i = 0; i < 10000; i++);
        std::cout<<"Display From MainThread"<<std::endl;
    threadObj.join();    
    std::cout<<"Exit of Main function"<<std::endl;
    return 0;
}

When I installed MinGW I selected to install threads but still get this error.

Quadsam
  • 3
  • 2
  • Looks like you have an old mingw that defaults to an older C++ Standard. Give `g++ -std=c++11 main.cpp -o main.exe` a shot to force compiling to the c++ 11 Standard. – user4581301 May 12 '20 at 03:07
  • 2
    Oh, and lose the semicolons on the end of the `for` loops. They separate the `for` from the line following it, so the following line doesn't loop. – user4581301 May 12 '20 at 03:08
  • Install mingw-w64 instead of mingw.org. Also [see here](https://stackoverflow.com/questions/37358856/does-mingw-w64-support-stdthread-out-of-the-box-when-using-the-win32-threading) – M.M May 12 '20 at 03:14

0 Answers0