0

I have tried compiling my C++ File with #include <thread> using G++, but it failed. Here is my source code below:

#include <iostream>
#include <thread>
#include <windows.h>

using namespace std;

void act_1()
{
    cout << "Hello World from act_1\n";
    return;
}

int main()
{
        thread th(act_1);
        th.join();
        
        ExitProcess(EXIT_SUCCESS);
}

Now, I have used the flags -std=c++11 and -pthreads, but it is still not working. Here is the console output:

C:\Users\Mayukh\Desktop>g++ -std=c++11 -pthread threaded.cpp -o thread
threaded.cpp: In function 'int main()':
threaded.cpp:15:3: error: 'thread' was not declared in this scope
   thread th(act_1);
   ^~~~~~
threaded.cpp:15:3: note: 'std::thread' is defined in header '<thread>'; did you
forget to '#include <thread>'?
threaded.cpp:4:1:
+#include <thread>

threaded.cpp:15:3:
   thread th(act_1);
   ^~~~~~
threaded.cpp:16:3: error: 'th' was not declared in this scope
   th.join();
   ^~
threaded.cpp:16:3: note: suggested alternative: 'tm'
   th.join();
   ^~
   tm

Please help me

Mayukh
  • 23
  • 6
  • Once you fix the `th` versus `th1` variable name discrepancy it's going to be hard to replicate your problem. – Some programmer dude Apr 11 '21 at 04:30
  • 4
    There is more than one flavour of g++ under windows, and you haven't specified which one. If you are using mingw, a dup of your question (with an answer) is at https://stackoverflow.com/questions/63582175/why-doesnt-my-compiler-recognize-include-thread-c – Peter Apr 11 '21 at 04:32

1 Answers1

0

I found the answer for now, as I was using x86_x64_win32, and it had no threading implemented, I switched to x86_x64_posix, and it is working for now.

If anyone still can find the solution in x86_x64_win32 at MinGW - W64, please post an answer

Mayukh
  • 23
  • 6