Delay for c++
Baconman321

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!

You are viewing a single comment. View All
Highwayman

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

mwilki7

@Highwayman
Why does portability matter for projects like these?

Highwayman

@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

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

mwilki7

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

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

Highwayman

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