CodeBlocks c++ - Can't use Thread because the compiler doesn't support it

5.7k views Asked by At

Firstly I wanted to create a program that would count the time from the user input time to zero, this was the code -

#include <iostream>
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <dos.h>
#include <windows.h>
#include <thread>
using namespace std;

//  sleep(5000);
int runTimer = 1;
int seconds;
int hoursLeft;
int minutesLeft;
int secondsCount=0;
void timeLeft ()
{
    hoursLeft = seconds/3600;
    minutesLeft = seconds/60 - hoursLeft*60;
}
void timer ()
{
    while(runTimer=1)
    {
         if (secondsCount == 60)
    {
    timeLeft();
    cout << "The Amount of time left is: " << hoursLeft << " hours and " << minutesLeft << " minutes left." << endl;
    secondsCount=0;
    }
    secondsCount++;
    seconds--;
    Sleep(1000);
    timer();
    }

}
int main()
{
    // introduction and time picking
    cout << "Welcome to my Timer - Please set the amount of hours and than minutes you want the timer to run" << endl;
    double requestedHours, requestedMinutes;
    cin >> requestedHours;
    cin >> requestedMinutes;
    double requestedSeconds = requestedHours*3600 + requestedMinutes*60;
    seconds = requestedSeconds;
    cout << "Timer Started";
    timer();
}

But than I wanted to add a function in which the user could type a word or a letter to pause the program, (and after a bit of reearching I found out about threading) - but when I added #include <thread> I got this massage -

"#error This file requires compiler and library support for the \
ISO C++ 2011 standard. This support is currently experimental, and must be \
enabled with the -std=c++11 or -std=gnu++11 compiler options.
#endif" 

How to fix this?

1

There are 1 answers

1
M.M On BEST ANSWER

You seem to be using g++ in Windows so I assume it is a flavour of MinGW that came with Code::Blocks.

GNU glibc doesn't support Windows threads (its dev team doesn't care about Windows) so you have to either use a MinGW pthread build, or use an add-on.

Firstly you should add -std=c++11 to your build options.

However, your error message suggests you have installed quite an old version of g++ so I would recommend upgrading to Mingw-w64 (an actively-maintained fork of Mingw). See here for installation help.

For more info about thread support in various versions of MinGW see this thread. I use Mingw-w64 with Win32 threads and the meganz addon in Code::Blocks successfully.