1

I have a problem including the thread library. The following code:

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

using namespace std;

//The function we want to make the thread run.
void task1(string msg)
{
    cout << "task1 says: " << msg;
}

int main()
{
    // Constructs the new thread and runs it. Does not block execution.
    thread t1(task1, "Hello");

    //Makes the main thread wait for the new thread to finish execution, therefore blocks its own execution.
    t1.join();
}

Produces these errors:

enter image description here

The code is taken from the answer to another stackoverflow question. I am fairly new to codeblocks and C++ so please explain to me what I am doing wrong.

jxh
  • 69,070
  • 8
  • 110
  • 193
  • Okay, it is indeed weird. What I'd probably wanted to know: can you find the header `thread` itself, and look at there? It would be in path `/usr/include/c++/SomeVersion/thread` in GNU/Linux, and, I guess, somewhere in `…/include/c++/SomeVersion/thread` in Windows® *(in the latter case you need to search a little)*. You have to find whether somewhere in that file a words `class thread`. From your problem I am guessing that the header somehow screwed. – Hi-Angel Jun 22 '15 at 18:48
  • @Hi-Angel: It appears the out of the box TDM compiler with Code::Blocks is compiled without thread support. You have to download a different compiler and use that with Code::Blocks instead to get thread support. – jxh Jun 22 '15 at 21:44
  • If you are using mingw-w64 with win32 threads then you need to use the [meganz threading addon](http://stackoverflow.com/a/27421968/1505939) – M.M Jun 22 '15 at 23:28

1 Answers1

0

You probably have not set proper flags for compiler (so that it uses c++11). Way of doing it in codeblocks

Community
  • 1
  • 1
psliwa
  • 1,094
  • 5
  • 9
  • In this case the compiler would complain about a support for C++11 not being enabled. I just even tested to be sure — compiled their example with `-std=c++03` flag. – Hi-Angel Jun 22 '15 at 18:26
  • 1
    No, that is everything that is being said when i run the code... Why wouldn't I post it all? It doesn't say anything about C++11 not being enabled. I enabled it the way Piotr said even before I posted this thread. The library is recognized when I type #include but whenever I try to use the thread function it doesn't show up – SkrewCodeBlocks Jun 22 '15 at 19:34
  • http://forums.codeblocks.org/index.php?topic=17841.0 – jxh Jun 22 '15 at 21:42
  • @SkrewCodeBlocks: Your original question did not show everything. I updated your question with the complete output. In the forums discussion link in my previous comment, there is a link to a MinGW build that has the std::thread support built-in. – jxh Jun 23 '15 at 00:48
  • @jxh by the way, I just found an interesting thing: in the forum referenced by link folks mentioned that the issue with missing `thread` class is tied to license issues. So, I just out of curiosity tried to compile the example with MinGW being installed from Ubuntu repository, and… it works! :/ So strange. – Hi-Angel Jun 23 '15 at 01:40
  • @Hi-Angel: It's a windows thing. – jxh Jun 23 '15 at 02:42