I’m using clock() from time.h to provide a rough time elapsed mechanism for an operation I want performed around once per second. (I probably want to use time() but the majority of the question stands since time() also returns -1 on a failure). It’s not critical to be precise since other thresholds are high enough that if this were out even up to a half second, it’d be ok.
The function returns -1 on a failure, and at present if I detect a failure I retry the clock up to 100 times over the next second.
Other than some sort of physical hardware failure, what reasons can cause clock() or time() to fail? If after the retries it is still -1, can I assume that the system is having a “bigger problem” (and so I may want to gracefully exit)?
My friendly man page states the following:
Do note that
clock()returns a value based on CPU time usage (on everything excluding Windows), if you want a wall clock time usetime()instead.