Ask coding questions

← Back to all posts
Delay for c++
h
Baconman321 (1096)

Does anyone know a repl.it compatible delay function for c++? I would like to use it to make the computer slow down, and make it easier for calculations. Thanks!

Comments
hotnewtop
mat1 (4429)

You can sleep in c++ with unistd.h and the sleep function. For example, here's a program that outputs foo, waits a second, and then prints bar:

#include <iostream>
#include <unistd.h>

int main() {
  std::cout << "foo\n";
  sleep(1);
  std::cout << "bar\n";
}
Highwayman (1483)

Please do not use the sleep function in unistd.h, it isn’t portable. Try sleep_for() in the thread header.

#include <thread> // this_thread, sleep_for()
#include <chrono>  // milliseconds

int main(){
  std::this_thread::sleep_for(std::chrono::milliseconds(1000);
}
mwilki7 (1134)

@Highwayman
Why does portability matter for projects like these?

Highwayman (1483)

@mwilki7 because of habits. If you develop a habit of only using a method that works on only a few OSs when there is a way that is entirely portable, you’re going to mess up one day. Also If you don’t even know it’s not portable, you could end up having there be an error and not knowing why on a different system. It’s different if it’s not something you can do with the standard library, though that I’d consider a skill. But otherwise it will just be a hindrance. Also if you use tons of different methods that aren’t standard, it will reduce code readability. For example, in using a function named 'sleep' I have no idea how long it will sleep for in terms of units. But on the other hand, if you use sleep_for() I can look that up on a cpp reference for the standard lib and immediately get my answer, or I could already know the answer.

It’s more than just portability, it’s about good and bad practices. I’m not saying what ya’ll are doing is evil or anything, but it is a little bit better this way.

Highwayman (1483)

@mwilki7 and besides, it’s more flexible than sleep. You can use different units.

mwilki7 (1134)

@Highwayman
For one, the compiler will tell you what's wrong if there are any undefined references or functions. It's hard to form a bad habit using a function when the program in question won't even compile on a different operating system.

Secondly, constantly checking what is portable for all libraries or functions is also a hindrance. Slowing down development time on something that will never release on other platforms is pointless. Portability is something that needs to be decided from the design stage. If there's no need for portability, anything done about it is just wasted time.

Thirdly, the unistd.h library IS standard for Unix operating systems. It has documentation for it just as C++ libraries do. Neither sleep() nor sleep_for() imply what unit of measurement is being used. The documentation will, hopefully, contain this information.

Fourthly, naming of things are much more impactful than whether or not they are standard.
Example:

sleep(one_second)
versus
sleep(1000)

Portability should only be a priority when something is intended to be portable. It isn't always better one way or the other.

Highwayman (1483)

@mwilki7 I meant why not what sry bout that.
1, it is better to not ever encounter the non-compiling at all, and it is faster to only memorize the - comparatively- small amount of standard solutions to the several different solutions for different systems. The point is, he will never ask this question again if he uses the standard way, specifically because it is everywhere. In the long run, it’s better.
2, sure, if you don’t know a portable way and don’t feel like looking it up, fine - but this is not the case, here he is asking us for the way, so he did take the time to 'look it up' and in showing him a standard way, he won’t have to continually 'look it up' for a different os. Again, easier/faster in the long run. The point here is that he is learnings something new, and if he learns a portable way to do it, it’s faster. Again.

I agree it should not be the top thing on your list when developing, but when learning it would be better to just learn the portable way. For the third time - not to be trashy, it’s just my point - it’s better in the long run.

I see where your coming from, and I agree in that situation yes - but I see a different situation.

mwilki7 (1134)

From: http://man7.org/linux/man-pages/man3/usleep.3.html

#include <unistd.h>

       int usleep(useconds_t usec);
The usleep() function suspends execution of the calling thread for
       (at least) usec microseconds.  The sleep may be lengthened slightly
       by any system activity or by the time spent processing the call or by
       the granularity of system timers.

Example program:

#include <iostream>
#include <string>
#include <unistd.h>

using namespace std;

int main()
{
    int one_second = 1000 * 1000;
    for (int i = 0; i < 5; i++)
    {
        cout << (i + 1) << endl;
        usleep(one_second);
    }
}
Baconman321 (1096)

@mwilki7 Thank you so much, however, I also want a program that will be able to print 1 letter after another. Like some type of computer in a sci-fi movie. Such as being able to print h-e-l-l-o. I tried your tactic and instead of printing h, and waiting 1 second, then goes to e and so on, it waited 5 seconds (1 for each of the letters), and printed hello as a whole word, not printing h, and going to e. Can you help me do this. The only solution I find is to print H, then do \n. I want to be able to print hello on a single line. I would gladly appreciate your help, and if this is not possible, please help me improvise.

mwilki7 (1134)

@Baconman321
You can wrap it inside a function.

#include <iostream>
#include <string>
#include <unistd.h>

// (50 * 1000) = 50 milliseconds
#define DEFAULT_PRINT_DELAY (50 * 1000)

using namespace std;

void special_print(string message, int delay=DEFAULT_PRINT_DELAY)
{
    for (char c: message)
    {
        cout << c << std::flush; // flush it so you see it printed immediately
        usleep(delay);
    }
    cout << "\n";
}

int main()
{
    cout << "Message: ";
    special_print("testtesttesttesttesttesttest");
}