定时发送间隔 [英] Timing send interval

查看:98
本文介绍了定时发送间隔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 我开发了一种软件,该软件使用C ++使用RTP协议发送媒体,每20毫秒发送一次数据包,并从光盘中读取文件.我的问题是定时发送间隔,我还没有找到一种好的方法来精确地等待20 ms而不会过度使用CPU资源.
现在我们都知道Sleep(20)可以在20ms之后的任何时间返回并且无法使用.我现在的操作方式是通过轮询计时器来进行,并且效果很好,但是令人讨厌的解决方案是它占用了很多CPU,而且很丑陋.
有人有建议吗?

在此先谢谢您.

Hi I have developed a software that sends media using the RTP protocol using C++, the packets is sent every 20ms, and the files are read from discs. My problem is timing the send inteval, I have not found a good way to wait exactly 20 ms without overusing CPU resources.
We all now that Sleep(20) could return any time after 20ms and can not be used. The way I do it now is by polling a timer, and that works perfectly, but a hate the solution cause it is using to much cpu and it is ugly.
Does anyone has a suggestion?

Thanks in advance.

推荐答案

您可以使用WaitableTimer.请参阅 CreateWaitableTimer() [ http://www.decuslib.com/decus/vmslt97a/ntstuff/timer.txt [ ^ ].

另请参见 Timers教程 [
You may use a WaitableTimer. See CreateWaitableTimer()[^] at MSDN and the example link [^].

If this is still too inaccurate you may tweak the timer resolution using the undocumented function NtSetTimerResolution() (use GetProcAddress() to get the function address from the Ntdll). See http://www.decuslib.com/decus/vmslt97a/ntstuff/timer.txt[^].

See also the Timers Tutorial[^].


您应该看看Windows Multimedia Timers,因此该功能在群组中:

You should take a look at Windows Multimedia Timers, thus the function in the group:

timeSetEvent
timeKillEvent

timeGetDevCaps



我个人还没有与他们合作,但是MS承诺它们可以更好地满足多媒体应用程序所需的精确计时要求.



I personally haven''t worked with them, but MS promises that they are way better for the precise timing required in multi-media applications.


Windows并非实时操作系统" .没有任何事件的时间保证.

即使您坐在循环中读取周期计数器以确定确切的时间已过20毫秒,您的进程仍可能会被一些晦涩的Windows进程抢占,这可能需要* SECONDS *才能完成并返回给您.

使用Windows无法获得如此准确的结果.

现在,您无需指定操作系统,并且在许多其他操作系统上也存在"sleep()"(尽管您确实评估了基于Windows调用WaitForSingleObject()的解决方案).但是大多数通用操作系统仍然无法提供完整的实时内核"效果,无法为您提供精确的计时.
Windows is not a "real time operating system". There are no guarantees on timing of any event.

Even if you sat in a loop reading the cycle counter to determine exactly when 20ms has passed, your process may still be preempted by some obscure Windows process that may take *SECONDS* to complete and return to you.

You can''t get that accurate with Windows.

Now granted that you did not specify the operating system and "sleep()" does exist on many other operating systems (although you did seem to evaluate a solution based on WaitForSingleObject(), a Windows call). But most generat purpose operating systems still do not provide a complete "real time kernel" effect that would give you the precise timing you are looking for.


这篇关于定时发送间隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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