0

I have been starting to try and learn how to utilize multithreading in c++, but the #include<thread> causes issues with the thread type being not declared in the scope. error: 'thread' was not declared in this scope. I have been doing research, and I have come across a lot of answers regarding how to solve this issue. Im presently at the understanding that my compiler, MinGW, doesn't support thread effectively, but I am not sure what to do with that information.

Any guidance on this matter is appreciated.

Also, I think this may be helpful. If I run gcc -v on my command line, I get this output:

Using built-in specs.
COLLECT_GCC=c:\MinGW\bin\gcc.exe
COLLECT_LTO_WRAPPER=c:/mingw/bin/../libexec/gcc/mingw32/6.3.0/lto-wrapper.exe
Target: mingw32
Configured with: ../src/gcc-6.3.0/configure --build=x86_64-pc-linux-gnu --host=mingw32 --target=mingw32 --with-gmp=/mingw --with-mpfr --with-mpc=/mingw --with-isl=/mingw --prefix=/mingw --disable-win32-registry --with-arch=i586 --with-tune=generic --enable-languages=c,c++,objc,obj-c++,fortran,ada --with-pkgversion='MinGW.org GCC-6.3.0-1' --enable-static --enable-shared --enable-threads --with-dwarf2 --disable-sjlj-exceptions --enable-version-specific-runtime-libs --with-libiconv-prefix=/mingw --with-libintl-prefix=/mingw --enable-libstdcxx-debug --enable-libgomp --disable-libvtv --enable-nls
Thread model: win32
gcc version 6.3.0 (MinGW.org GCC-6.3.0-1)

edit: I've seen this webpage, https://github.com/meganz/mingw-std-threads, as a potential solution, but I don't think this works for me. Unless somehow I am putting the mingw-thread.h in the wrong folder.

T. Smith
  • 124
  • 8
  • Possible duplicate of [Does MinGW-w64 support std::thread out of the box when using the Win32 threading model?](https://stackoverflow.com/questions/37358856/does-mingw-w64-support-stdthread-out-of-the-box-when-using-the-win32-threading) – druckermanly Mar 15 '19 at 03:14
  • Post your actual code, otherwise known as an [MCVE](https://stackoverflow.com/help/mcve). I suspect it's something subtle and easy in your code. – selbie Mar 15 '19 at 05:18

2 Answers2

1

I think the problem here is you did not tell the compiler using c++11 features. 'thread' is belonged to c++11 features, let try adding -std=c++11 into CXXFLAGS or CPPFLAGS and see if it resolves your problem

selbie
  • 100,020
  • 15
  • 103
  • 173
HungNV
  • 11
  • 3
  • You mean to compile a program with "g++ -std=c++11 test.cpp"? Because no that doesn't work unfortunately. – T. Smith Mar 15 '19 at 04:30
  • Here is problem: - `thread` is belong to name space `std`, so without name space `std` it will lead to your error. - If you declare your variable as `std:thread` and compiler gives error as `thread` is not member of `std`, it cause of with thread model win32, g++ is lack of gthread library, so it cannot manipulate class `std::thread`. So solution is: *Install gthread library with mingw, add include gthread header above `#include ` *Using substitute solution as you found above *Re-install and using posix thread model – HungNV Mar 20 '19 at 03:51
0

My psychic powers suggest your forgot the std:: namespace attribute. That would explain why thread is undefined, even with #include <thread>. The other answer about -std=c++11 is also steering you into the right direction. And don't forget the -pthread compiler/linker option.

$ cat foo.cpp
#include <thread>
#include <iostream>

void threadfunc()
{
   std::cout << "Hello from the worker thread" << std::endl;
}


int main()
{
   std::thread t(threadfunc);
   t.join();
   return 0;
}

$ g++ foo.cpp -std=c++11 -o foo -pthread
$ ./foo
Hello from the worker thread
selbie
  • 100,020
  • 15
  • 103
  • 173
  • Using that file in addition to the compilation command you suggested still does not help. The error with that is "error: 'thread' is not a member of 'std'", and also there's an obvious "t was not declared in this scope", but yeah. I've tried that and no luck – T. Smith Mar 15 '19 at 05:15
  • You're running `g++` as your compiler command line and not `gcc`, right? And your source file extension is `.cpp` instead of something else? – selbie Mar 15 '19 at 05:20
  • Yes exactly. I am using Visual studio codes built-in terminal. I cd to the directory containing my Testing.cpp which currently is a copy of your file from above. In the terminal command line, I run "g++ Testing.cpp -std=c++11 -o testing -pthread" and get the two errors. – T. Smith Mar 15 '19 at 05:24