0

I've been having an error declaring a thread class object. I tried this code and it gave me the error "'thread' was not declared in this scope". If it helps, I'm compiling to Windows using MinGW GCC.

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

void func(){
    cout << "Hello from thread 2\n";
}
int main(){
    cout << "Hello from thread 1\n";
    thread t2(func);
    t2.join();
    return 0;
}

Compiling gives me "error: 'thread' was not declared in this scope"

  • ***I'm compiling to Windows using MinGW GCC*** Too old a version? The current MinGW has gcc-11.2 – drescherjm May 27 '22 at 23:36
  • @drescherjm, I just got my MinGW from SourceForge. maybe there is a better place to get it? My MinGW installer only shows an option for gcc-6.3.0-1. Is mingw-w64.org legit? – Jaihson Kresak May 27 '22 at 23:41
  • Did you turn on C++11 or higher? – NathanOliver May 27 '22 at 23:44
  • I assumed it would automatically compile c++11. The thread file does exist and defines the thread class.. – Jaihson Kresak May 27 '22 at 23:49
  • possible dupe: https://stackoverflow.com/questions/21211980/mingw-error-thread-is-not-a-member-of-std – NathanOliver May 27 '22 at 23:49
  • The compiler defaults to the stone age, enabling c++11 just gets you to the bronze age. Join the modern age, use -std=c++20 and enjoy having `std::jthread`. – Goswin von Brederlow May 27 '22 at 23:52
  • thread class seems to be in the std namespace, according to the thread file I have. The MinGW-builds-install.exe file referenced by @NathanOliver gives errors... – Jaihson Kresak May 27 '22 at 23:57
  • *"I assumed [...]"* -- first or second rule of debugging: question all assumptions! – JaMiT May 27 '22 at 23:57
  • *"My MinGW installer only shows an option for gcc-6.3.0-1"* -- is that what you actually have installed? The default for gcc 6.1 through 10 is C++14. You might want to check what you are actually using to compile. Look at the command used to compile for any "-std" options and also run `gcc --version` to verify what version you are actually running. – JaMiT May 28 '22 at 00:03
  • gcc --version return gcc-6.3.0-1. I just tried Cygwin gcc (version 11 point something) and it gave some big long error – Jaihson Kresak May 28 '22 at 00:13
  • 11. anything shouldn't have a problem, and cygwin is a POSIX compatibility layer, so if it doesn't have threading over Pthreads, I'll be a monkey's uncle. Mind you, I just might be. My nephew needs to be seen to be believed. Give us real code and the error messages and someone here can help you out. – user4581301 May 28 '22 at 02:35
  • 1
    Ditch your MinGW version and download [MSYS2](https://stackoverflow.com/q/30069830/2752075). They usually have a more up-to-date version than mingw-w64.org, and the package manager with prebuilt libraries is good. – HolyBlackCat May 28 '22 at 13:55
  • I am using msys2 to install mingw and the builtin package management to install many open source libraries. – drescherjm May 29 '22 at 03:09

1 Answers1

-2

Threads are missing in mingw, but you can use some library like this one mingw-std-threads.

donwiktorb
  • 11
  • 2
  • Threads are missing in some Stone Age distributions of MinGW. I haven't had a problem in the last six or seven years, but some of those hoary old beasts just won't die. Shouldn't be a surprise seeing as we still get Turbo C++ questions. – user4581301 May 28 '22 at 02:31