什么情况会导致clock()返回-1(即失败)?
我使用 time.h 中的 Clock() 为我想要每秒执行一次的操作提供一个粗略的时间流逝机制。 (我可能想使用 time() 但大多数问题仍然存在,因为 time() 在失败时也会返回 -1 )。精确度并不重要,因为其他阈值足够高,即使超过半秒也没关系。
该函数在失败时返回 -1,目前如果我检测到失败,我会在下一秒重试时钟最多 100 次。
除了某种物理硬件故障之外,还有哪些原因会导致 Clock() 或 time() 失败?如果重试后它仍然是-1,我可以假设系统有“更大的问题”(所以我可能想优雅地退出)?
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)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我的友好的手册页指出以下内容:
请注意,
clock()
返回一个基于 CPU 时间使用情况的值(在除 Windows 之外的所有系统上),如果您想要挂钟时间,请使用 <代码>time() 相反。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.来自
人钟(3)
听起来符合标准的实现可以只返回该值并完成它。
From
man clock(3)
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.