开发者

What conditions can cause clock() to return -1 (ie fail)?

开发者 https://www.devze.com 2023-03-30 04:19 出处:网络
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

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 return开发者_StackOverflows -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:

If the processor time used is not available or its value cannot be represented, the function shall return the value (clock_t)-1

Do note that clock() returns a value based on CPU time usage (on everything excluding Windows), if you want a wall clock time use time() instead.


From man clock(3)

If the processor time used is not available or its value cannot be represented, the function returns the value (clock_t)-1

Sounds like a conformant implement could just return that and be done with it.


Calling the function from a kernel mode application can also cause it to fail, which was what I was concerned about.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号