跨平台毫秒计时器持续超过 49 天?

发布于 2024-08-28 05:15:05 字数 267 浏览 5 评论 0原文

我将用 C/C++ 开发一个小型专用服务器,这将需要永远的正常运行时间。我一直在研究一些时间函数,因为计算需要毫秒计时。我面临 2 个问题:

  1. 使用 32 位整数来存储自操作开始以来的毫秒数将在大约 49 天标记重置为零时回绕。我曾考虑过使用 64 位整数,使用 gettimeofday 来检索微秒,但这将我带到了第二部分。

  2. 似乎没有任何标准系统调用来获取与平台无关的经过的毫秒数

我应该做什么解决这两个问题吗?

I'm going to be developing a small dedicated server in C/C++ that will require uptime of forever. I've been looking into some time functions as millisecond timing is required for calculations. I have 2 problems that I'm facing:

  1. Using a 32bit integer to store the number of milliseconds since the operation began will wrap around at about the 49 days mark resetting to zero. I have thought about using 64 bit integers, using gettimeofday to retrieve microseconds but this brings me to the second part.

  2. There doesn't seem to be any standard system calls for getting elapsed milliseconds that are platform independant

What should I do to resolve both these issues?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

紫﹏色ふ单纯 2024-09-04 05:15:05
  1. 使用 64 位整数,假设给你足够的时间

  2. 你是对的;没有标准。一种可能是使用 Boost DateTime 库,交替寻找另一个或自己推出。

祝你好运!

  1. Use a 64bit integer, presuming that gives you enough time

  2. You are correct; there is no standard. One possibility would be to use the Boost DateTime library, alternately find another or roll your own.

Good Luck!

手心的海 2024-09-04 05:15:05

正如已经说过的,您将面临的第一个问题是获得可靠的毫秒级精确时间。

我承认我对这个问题有点分阶段。

我可以理解对精确计时(毫秒级,甚至微秒)的需求,但以毫秒级计时 50 天似乎……很奇怪。

您也许应该首先检查您的需求,但很少需要超过 6 或 7 个有效数字......而且我担心您正在尝试获得一个适合所有持续时间对象的尺寸。

也许您应该对您的持续时间进行分类:

  • 最多几分钟> 否则使用毫秒精度
  • >使用秒精度(自 1970 年 1 月 1 日以来著名的计数)

因为... 1/10 秒在 2 个月的尺度上有什么意义?

As has already been said, the first problem you are going to confront is going to obtain a reliable millisecond-precise time.

I admit I am a bit phased by the question though.

I can understand the need for precise timing (millisecond level, even microsecond) but timing a 50days at a millisecond level seems... strange.

You should perhaps review your need first, but it is rare to need more than 6 or 7 significant digits... and I am afraid that you are trying to get a one size fit them all duration object.

Perhaps that you should instead classify your durations:

  • a few minutes at most > use millisecond precision
  • otherwise > use second precision (the famous count since Jan 1st 1970)

Because... what is the sense of 1/10 second at the scale of 2 months ?

唠甜嗑 2024-09-04 05:15:05

明显的。使用 64 位整数和特定于平台的代码来获取毫秒数。在 Unix(包括 OSX)上,您需要 gettimeofday。在 Windows 上,祝您好运,获得可靠的毫秒粒度时间源; Tcl 中的 代码库要做到这一点确实很复杂,因为该地区存在一些邪恶的陷阱。

Obvious. Use 64-bit integers with platform-specific code to get the number of milliseconds. On Unix including OSX, you want gettimeofday. On Windows, good luck getting a reliable millisecond-granularity time source; the code in the Tcl library to do this is really complex as there are some evil gotchas in the area.

瑾夏年华 2024-09-04 05:15:05

答案 1:如果您测量的“毫秒计时”大约在 20 天以下,您可以将时间作为无符号值减去,然后检查结果作为有符号值。这应该通过包装计时器(从 0xffffffff 包装到 0x00000000)给出正确的结果。如果您的时间超过 20 天,则需要更多位。

answer to 1: If the "millisecond timing" that you are measuring is under around 20 days, you can subtract the times as unsigned values and check the result as signed value. This should give the right result with wrapping timers (wrapping from 0xffffffff to 0x00000000). If your timing is over 20 days, you need more bits.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文