0

I am new to multithreading but I found an answer in this site about simple multithreading code. I copied it exactly with #include <thread> but later it says in the terminal (I am running Visual Studio Code) that I have not included thread. Here is the code.

#include <string>
#include <iostream>
#include <thread>

using namespace std;

void task1(string msg)
{
    cout << "task1 says: " << msg;
}

int main()
{

    thread t1(task1, "Hello");

    t1.join();
}

This is the error I see:

c:\Users\Lol\Desktop\C++\Thread testing\Threadtest.cpp: In function 'int main()':
c:\Users\Lol\Desktop\C++\Thread testing\Threadtest.cpp:18:5: error: 'thread' was not declared in this scope
   18 |     thread t1(task1, "Hello");
      |     ^~~~~~
c:\Users\Lol\Desktop\C++\Thread testing\Threadtest.cpp:5:1: note: 'std::thread' is defined in header '<thread>'; did you forget to '#include <thread>'?
    4 | #include <thread>
  +++ |+#include <thread>
    5 |
c:\Users\Lol\Desktop\C++\Thread testing\Threadtest.cpp:24:5: error: 't1' was not declared in this scope; did you mean 'tm'?
   24 |     t1.join();
      |     ^~
      |     tm
The terminal process terminated with exit code: 1
Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
LuckyFire
  • 41
  • 7
  • 1
    Did you turn on C++11 or higher? – NathanOliver Jun 09 '20 at 20:23
  • yes i did. Why? – LuckyFire Jun 09 '20 at 20:24
  • What's the exact error messages(s) you get? – 1201ProgramAlarm Jun 09 '20 at 20:24
  • See if this helps - https://stackoverflow.com/questions/17901797/c-compiler-not-finding-include-thread – Abhishek Bhagate Jun 09 '20 at 20:24
  • 1
    `std::thread` only works in C++11 or higher. Please post the full error message that you get. – NathanOliver Jun 09 '20 at 20:24
  • ok here it is in pastebin: https://pastebin.com/HhVDupsu – LuckyFire Jun 09 '20 at 20:27
  • The code is fine, it should compile with C++11 or later. The error doesn't complain about `#include ` but instead about the identifier `thread` which is odd. Whatever the problem is, the cause is not shown in what you provided. Either you have an odd project configuration or the code you provided is not representative of the code that is causing the problem. – François Andrieux Jun 09 '20 at 20:30
  • Here is the full code: https://pastebin.com/sjrUNzJy – LuckyFire Jun 09 '20 at 20:32
  • 2
    At a guess you are using a version of MinGW that doesn't support `std::thread` – Alan Birtles Jun 09 '20 at 20:33
  • Here is my version. gcc version 9.2.0 (MinGW.org GCC Build-20200227-1) – LuckyFire Jun 09 '20 at 20:36
  • Here you can see that the code is correct https://wandbox.org/permlink/v1BPcno7hjIj24kd The problem is somewhere else. Can you build your project from terminal? – Thomas Sablik Jun 09 '20 at 20:39
  • Yes I found that it is a problem with MinGW. Probably because it is 64 bit. Do you know how to install clang. Are there any online tutorials. Thanks – LuckyFire Jun 09 '20 at 20:46
  • I follow in installation process [similar enough to this just recommend it](https://stackoverflow.com/questions/30069830/how-to-install-mingw-w64-and-msys2). Never had any problems and as an added bonus msys provides an excellent ecosystem of extra tools and libraries. – user4581301 Jun 09 '20 at 20:52
  • Is that an installation process for clang. I already have mingw installed. I am really a noob so sorry if this is a dumb question. – LuckyFire Jun 09 '20 at 21:00
  • Sorry, no that's a process for MinGW's g++, the clang install isn't much different. You want the mingw-w64-clang package. I'd take its g++ package as well though. There's something a bit off with your current g++ install. – user4581301 Jun 09 '20 at 23:12

0 Answers0