-2

I am a novice in C++ and I am struggling to make my program to wait a few minutes before executing a function.

I know there are lots of topics about it but I have a problem with my compiler. I can't seem to use the boost library nor the thread library. And since I can't use the thread library, I can't use the chrono library either.

I am using GNU GCC Compiler. I have MinGW installed. Is it outdated or something? What is the best compiler to code in C++? My OS is Windows.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
user3757605
  • 442
  • 1
  • 9
  • 20

2 Answers2

1

You could use this

#include <unistd.h>
...
usleep(1000); // Time in microseconds

or

 #include "stdafx.h"
    #include "windows.h"
    #include "iostream"
    using namespace std;
    int main(){
    int sleepTime = 1000;
    Sleep(sleepTime);
     return 0;
    }
WAQ
  • 2,556
  • 6
  • 45
  • 86
1

<thread> is only available starting with C++11.

It's likely you don't have the proper flags to tell GCC you want to enable C++11 support, which is disabled by default.

The command line parameter is -std=c++11.

Then, you can use std::this_thread::sleep_for() for cause your program to fall asleep. Note that if you only have one thread in your program, it will probably stop responding to user actions during that time.

Steve
  • 6,334
  • 4
  • 39
  • 67
  • I enabled the C++11 flag and I don't get an error that thread library is not found anymore. However I still do get an error saying 'std::this_thread' has not been declared'. – user3757605 Sep 06 '15 at 17:58
  • Are you using Eclipse? It's possible you need to enable the flag in two places - once for Eclipse's parser, and one for the compiler. See [this answer](http://stackoverflow.com/a/9135135/425871) for some details about that, it might help you if this is the case. – Steve Sep 06 '15 at 18:02