如何保证确切的线程睡眠间隔? [英] How to guarantee exact thread sleep interval?

查看:84
本文介绍了如何保证确切的线程睡眠间隔?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常,如果我想模拟一些工作或等待确切的时间间隔,请使用 condition_variable :: wait_for 或最差的 thread :: this_thread: :sleep_for 。但是 condition_variable 文档指出 wait_for wait_until 方法的阻塞时间可能比请求的时间更长。

Usually if I want to simulate some work or wait exact time interval I use condition_variable::wait_for or at the worst thread::this_thread::sleep_for. But condition_variable documentation states that wait_for or wait_until methods may block longer than was requested.


此功能可能由于调度或资源争用延迟而比timeout_duration更长

如何保证确切的等待间隔?

How exact wait intervals can be guaranteed?

更新

是否可以在没有 condition_variable的情况下到达它

推荐答案

您不能这样做。

要获得这样的确切保证,您需要一个实时操作系统。

In order to have exact guarantees like this, you need a real time operating system.

C ++不保证您使用的是实时操作系统。

C++ does not guarantee you are on a real time operating system.

因此它提供了典型的非RTOS所提供的保证。

So it provides the guarantees that a typical, non-RTOS provides.

请注意,编程还有其他复杂性

Note that there are other complications to programming on a RTOS that go far beyond the scope of this question.

实际上,一件事是人们真正想要细粒度的时序控制时(例如,他们在四处闲逛使用每帧或每条扫描线缓冲区之类的缓冲区,音频缓冲区之类的缓冲区执行的操作是检查时间是否短,以及是否如此旋转等待。如果时间更长,他们等待的时间要少于他们想要等待的时间,然后醒来并旋转。

In practice, one thing people when they really want fine-grained timing control (say, they are twiddling around with per-frame or per-scanline buffers or the like, or audio buffers, or whatever) do is check if the time is short, and if so spin-wait. If the time is longer, they wait for a bit less than the amount of time they want to wait for, then wake up and spin.

在RTOS上,该平台可能会提供所需的原语。这些不在标准C ++的范围内。据我所知,没有典型的台式机操作系统是RTOS。如果您正在为战斗机的控制硬件或类似程序进行编程,则可能是在RTOS上进行编程。

On a RTOS, the platform may provide primitives like you want. These lie outside the scope of standard C++. No typical desktop OS is an RTOS that I am aware of. If you are programming for a fighter jet's control hardware or similar, you may be programming on an RTOS.

我希望您不要编写战斗机控制软件并询问这个问题。堆栈溢出问题。

I hope you aren't writing fighter jet control software and asking this question on stack overflow.

这篇关于如何保证确切的线程睡眠间隔?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆