在准确的时间运行代码,类似于 crond/atd

发布于 2024-12-10 17:57:50 字数 97 浏览 0 评论 0原文

假设我正在实现一个时钟,并且我想最小化每单位时间的唤醒次数。因此,我希望仅在整分钟被唤醒(当我需要更新显示时)。在 C(或 C++)中执行此操作(最好是可移植的)的最佳方法是什么?

Suppose I'm implementing a clock, and I want to minmise the number of wakeups per unit time. So, I'd like to be woken up on full minutes only (when I need to update the display). What's the best way to do this (portably, preferably) in C (or C++)?

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

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

发布评论

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

评论(1

彡翼 2024-12-17 17:57:50

C++03 和 C07 标准并没有真正提供任何可移植的方法来执行此操作。如果您不能依赖较新的编译器,那么几乎您唯一合理的可移植选择可能是 POSIX 线程(下面将详细介绍)。

在 C++11 下,您可以创建一个线程,并使用 std::sleep_until 或(如果您愿意,出于某种原因)std::sleep_for。选择后者的一个原因是对 C 的可移植性。C1x 有一个 thrd_sleep 函数,本质上与 std::sleep_for 类似,因此它本质上可能更容易使用两者的功能相同。

可移植性(在这种情况下)可能很难定义。这些函数位于 C++ 标准和 C 草案中,但它们显然都是非常新的,因此很难猜测您关心的编译器是否真正实现了它们。同时,它们相当紧密地基于 pthreads,因此与它们大多是新的和不同的相比,它们已经(或很快)实现的可能性可能更大。

如果您愿意将自己限制在 C++ 上,您也可以考虑 Boost.thread。它与标准中的内容非常相似,但它已经可用于几乎所有主要的编译器和平台。

如上所述,另一种可能性是直接使用 POSIX 线程。从理论上讲,这可能不太便携(至少从长远来看),但实际上它现在可能至少是可移植的——特别是,可能可以移植到您真正关心的所有平台,即大多数真正重要的事情。显然,它们基本上可以在所有类 Unix 平台上使用,并且还有一个移植到 Win32

我可能应该添加一个我认为很小的警告:这些都不能真正保证在“准确的时间”运行。它们几乎可以保证您的线程将休眠至少与您指定的时间一样长的时间,但也可能更长——但 atcron 也是如此。大多数典型的操作系统都不是“实时”的,因此本质上任何在精确时间运行的尝试都是没有希望的,除非您非常宽松地定义“精确”。大多数人主要感兴趣的是足够接近,以便它看起来对用户来说是正确的,因此任何比十分之一秒左右更准确的东西都是不必要的。在这种情况下,您通常可以做得很好(尽管如果系统负载确实很重,即使可能是一个问题)。

The C++03 and C07 standards don't really provide any portable way to do this. If you can't depend on a newer compiler, nearly your only reasonably portable choice is probably POSIX threads (about which more below).

Under C++11, you can create a thread, and use std::sleep_until, or (if you prefer, for some reason) std::sleep_for. One reason to prefer the latter would be portability to C. C1x has a thrd_sleep function that's essentially similar to std::sleep_for, so it might be a little easier to use essentially the same function in both.

Portability (in this case) may be hard to define. These functions are in the C++ standard and C draft, but those are both obviously very new so it's hard to guess whether the compilers you care about actually implement them yet. At the same time, they're based fairly closely on pthreads, so there's probably a better chance of seeing them implemented already (or soon) than if they were mostly new and different.

If you're willing to restrict yourself to C++, you might also consider Boost.thread. It's pretty similar to what's going into the standard, but it's already available for nearly all the major compilers and platforms.

As mentioned above, another possibility would be to use POSIX threads directly. In theory that may not be quite as portable (at least in the long run), but in fact it's probably at least as portable right now -- and, in particular, may be portable to all the platforms you really care about, which is most of what really matters. Obviously they're available on essentially all Unix-like platforms, and there's also a port to Win32.

I should probably add a caveat that I'd guess is pretty minor: none of these really guarantees running at an "exact time". They pretty much guarantee that your thread will sleep for at least as long as you specify, but it could be longer -- but that's also true with at and cron as well. Most typical operating systems aren't "real time", so essentially any attempt at running at an exact time is hopeless, unless you define "exact" pretty loosely. Most people are primarily interested in being close enough that it looks and seems right to the user, so anything more accurate than a tenth of a second or so is unnecessary. In that case, you can generally do pretty well (though if the system is really heavily loaded, even that could be a problem).

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