-1
#include <iostream>
#include <thread>

using namespace std;

void thread_c() {
    for (int i = 0; i < 11; ++i) {
        cout << i;
    }
}

int main() {
    thread t(thread_c);
    t.join();
    return 0;
}

Such simple example, but it doesn't work on my g++ (MinGW.org GCC-6.3.0-1) 6.3.0, Windows 8.1 Here's the error:

main.cpp: In function 'int main()':
main.cpp:13:5: error: 'thread' was not declared in this scope
     thread t(thread_c);
     ^~~~~~
main.cpp:14:5: error: 't' was not declared in this scope
     t.join();
     ^

*I've been working on a big project, and noticed about some compilation errors. I wrote this code for testing my compiler out

What's wrong with it?

  • ¿How do you compile this example? – user7860670 Feb 13 '21 at 13:29
  • @user7860670 g++ -std=c++11 main.cpp -o test.exe – Andrew Lushin Feb 13 '21 at 13:32
  • 2
    Perhaps your installation of mingw is using win32 threads instead of posix threads and therefore does not provide ::std::thread facilities. See https://stackoverflow.com/questions/17242516/mingw-w64-threads-posix-vs-win32 – user7860670 Feb 13 '21 at 13:39
  • I am not an expert in c++ or compiling by any means but I have been working on a c++ program with a few friends and i remember we also had some thread issues. We added `-pthread -Wl,--whole-archive -lpthread -Wl,--no-whole-archive` when compiling. Maybe it will also solve some issues for you. – Finn Eggers Feb 13 '21 at 18:05

1 Answers1

0

Loking at your code and the compiler results I see the following:

  • #include <thread> is not including a file that defines thread as your code expects
  • GCC 6.3.0 is very old (current is 10.2.0), and chances are it was built without POSIX thread support (and probably only support for Windows threads)

I always recommend using MinGW-w64 instead of MinGW as it is more up to date and supports both 32-bit and 64-bit Windows.

When I build your example with the MinGW-w64 from http://winlibs.com/ (which has POSIX thread support) I get no errors and the example works (output is 012345678910).

Brecht Sanders
  • 2,726
  • 1
  • 8
  • 18