Clock() 返回负值?
我创建了一个服务器程序,并且函数“clock()”返回负值(并使服务器崩溃)的情况下发生了两次特定错误。两次都发生在程序在 Windows 32 位 VPS 上运行超过 100 小时的情况下。
这是我在 main.cpp 中的设置(在适当的地方进行剪切):
while (1) {
Sleep(STEP);
//execute main code like connection handling, AI, etc.
//check for clock error
if (clock() < 0) {
//error saved here
//close server
return 0;
}
}
就是这样,非常简单。 Clock() 在程序的其余部分中被广泛使用,因此当它出现这样的故障时,会导致很多问题。
我想知道,为什么它返回负值,我该如何修复它?
谢谢。
I have created a server program, and a certain error has occurred twice where the function 'clock()' has returned a negative value (and crashed the server). Both times it occurred when the program had been running for more than 100 hours, on a Windows 32-bit VPS.
Here's the setup I have in main.cpp (cut where appropriate):
while (1) {
Sleep(STEP);
//execute main code like connection handling, AI, etc.
//check for clock error
if (clock() < 0) {
//error saved here
//close server
return 0;
}
}
That's it, pretty simple. clock() is used throughout the rest of the program extensively, so when it glitches up like this it causes a lot of problems.
I am wondering, why does it return a negative value, and how can I fix it?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据 Microsoft 文档,如果“已过去的时间量”,它可以返回 -1时间不可用”。不幸的是,他们没有解释为什么时间不可用。
clock_t
的定义是long
,它是一个 32 位有符号值 - 它在溢出之前可以容纳 2**31。CLOCKS_PER_SECOND
的值为 1000,因此应该可以使用 596 小时。According to Microsoft's documentation it can return -1 if "the amount of elapsed time is unavailable". Unfortunately they don't explain how the time would be unavailable.
The definition of
clock_t
islong
which is a 32-bit signed value - it can hold 2**31 before it overflows. The value ofCLOCKS_PER_SECOND
is 1000, so it should be good for 596 hours.