Ask coding questions

← Back to all posts
Delay for c++

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


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:


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


Why does portability matter for projects like these?


@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.


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


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.

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


@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.



Example program:


@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.


You can wrap it inside a function.