1

I created a simple program to learn how to use threading. This is the code i created

#include <iostream>
#include <stdlib.h>
#include <thread>

using namespace std;

void count_asc();
void count_desc();

int main() {
    thread first(count_asc);
    thread second(count_desc);

    first.join();
    second.join();

    system("pause");
    return 0;
 }

void count_asc(){
    int ctr;
    for(ctr=1;ctr<=10;ctr++){
        cout<<"First thread: "<<ctr<<endl;
    }
}

void count_desc(){
    int ctr;
    for(ctr=10;ctr>=1;ctr--){
        cout<<"Second thread: "<<ctr<<endl;
    }
}

I am using Dev C++ 5.5.3. I have read other questions about this but me being a beginner in programming cannot really understand advanced instructions. When this code is compiled the following error is produced

main.cpp: In function 'int main()':
main.cpp:11:2: error: 'thread' was not declared in this scope
main.cpp:11:9: error: expected ';' before 'first'
main.cpp:12:9: error: expected ';' before 'second'
main.cpp:14:2: error: 'first' was not declared in this scope
main.cpp:15:2: error: 'second' was not declared in this scope

I have already included -std=c++11 in the c++ compiler additional command-line options in the project option of the Dev C++ but i can't still remove the errors. Can you please check what i am doing wrong? also as much as possible i dont want to start using other libraries as i am having a hard time building them(ex. boost)

JBentley
  • 6,099
  • 5
  • 37
  • 72
John
  • 11
  • 1
  • 2

1 Answers1

1

The problem is most likely due to lack of support for C++11's std::thread in the build of GCC 4.7.1 included with TDM-GCC. Have a look at this question, for details. Your code compiles fine with GCC 4.8.1 (although it still has runtime errors):

http://ideone.com/oUhvi3

I would therefore suggest that to resolve your problem, you try updating to a more recent version of the compiler. According to this link and this link doing so should be a simple matter of installing the most recent version of the compiler into the folder where it currently resides, or alternatively, installing it in a new folder and updating the settings in Dev C++ to point to the new compiler.

However, since you are new to C++ (and programming in general), and therefore have no particular attachment to Dev C++, I would recommend that you instead switch to a more modern and widely used IDE. MS Visual Studio is a good bet for Windows, but there are plenty of open source and cross platform IDEs available for C++. Using an IDE which is popular is recommended for beginners, as you are much more likely to find sources of help and support online, and more likely to get answers on sites such as Stackoverflow, when you get stuck. There are tons of Stackoverflow questions relating to IDEs. Examples (from a quick Google search):

What is the good cross platform C++ IDE?

Best C++ IDE or Editor for Windows

https://stackoverflow.com/questions/535369/what-is-the-best-free-windows-c-ide-compiler

Community
  • 1
  • 1
JBentley
  • 6,099
  • 5
  • 37
  • 72