Custom sleep() function using only standard libraries?

I heard that there is no sleep function in the standard libraries.

Because there's no "standard" way of sleeping.

Can you educate me on this?

Er OS theory. Letting a piece of code wait without wasting too much resources is one of the primary problems operating systems solve. Not wasting CPU time while a task is sleeping often means running some other piece of code in the meantime. Consequently, "sleeping" in a multi-tasking OS environment pretty much always means asking to OS to suspend your task and wake it up at a later time.

  1. how would one prevent the computer from wasting resources

OS theory.

  1. ... or do terminals have some kind of fps?

and basics of GUI programming.

Assuming the environment is Linux, pretty much the only relevant function from time.h would be nanosleep(), which is "standard" in a way (that is, there is at least one standard that describes it). However, it is very likely that select/ppoll/epoll or setitimer might be a better fit.

I mean, how to make some counter that is independent of processor speed?

https://en.wikipedia.org/wiki/Electronic_oscillator (truly independent)
https://en.wikipedia.org/wiki/Phase-locked_loop (the real answer in most cases)

/r/C_Programming Thread