了解多媒体定时器的奇怪行为 [英] Understanding the strange behavior of multimedia timer

查看:36
本文介绍了了解多媒体定时器的奇怪行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序 (C# .NET) 中使用多媒体计时器来提高计时器的准确性并实现 1 毫秒的计时器频率.到目前为止,我的应用程序一直运行良好,直到最近它开始出现奇怪的行为.我试图了解我的应用程序有什么问题.以下是采取的步骤

I am using Multimedia timers in my application (C# .NET) to increase accuracy of my timer and to achieve 1 ms timer frequency. My application had been working great so far until recently it started behaving strangely. I am trying to understand what is wrong with my application. Below are the steps taken

  1. 定时器频率设置为 1 毫秒,每 1 毫秒调用一次回调
  2. 有 4 个线程,每个线程都创建自己的计时器对象.它们都设置为在 1ms 后调用回调.这些是单独的实例,不共享.
  3. 旧代码执行时间约为 0.3 毫秒.在下一步之前,这一切正常.
  4. 应用程序代码已更改.定时器回调函数现在需要大约 1.2 毫秒的执行时间.这显然是一个问题.(我将在稍后优化代码.但现在我只想了解多媒体计时器的行为)
  5. 只有第一个线程调用计时器回调,而对于其他线程,回调仅调用两次或三次,此后不再调用回调.
  6. 看起来对于其他线程,计时器甚至被错过(?)并且无法赶上.(每次中断都会错过).

你能解释一下计时器对象的行为吗?由于它是单个进程,所有线程实际上都指向同一个计时器对象吗?为什么其他线程没有调用定时器回调?

Could you please explain me the behavior of the timer objects. Are all the threads actually pointing to same timer object since its a single process? Why are other threads not calling the timer callback?

推荐答案

多媒体定时器的最大分辨率为 1 毫秒.这会导致可编程中断控制器(在硬件上)每 1 毫秒触发一次.如果您启动 4 个线程,这些线程都创建了具有 1 毫秒计时的计时器,这并不意味着您每毫秒会收到一次以上的事件.

The maximum resolution for the Multimedia timer is 1ms. This causes the programmable interrupt controller (on the hardware) to fire every 1ms. If you fire up 4 threads that all create timers which have 1ms timings that does not mean you will get events more than once per millisecond.

我鼓励您阅读 为什么多媒体定时器 API (timeSetEvent) 没有我预期的那么准确? MSDN 上的博文.

I encourage you to read the Why are the Multimedia Timer APIs (timeSetEvent) not as accurate as I would expect? blog post on MSDN.

一些适用于此的引语(强调我的):

Some quotes that are applicable here (emphasis mine):

MM 定时器 API 允许开发人员重新编程可编程机器上的中断控制器 (PIC).您可以指定新定时器分辨率.通常,我们会将其设置为 1 毫秒.这个是定时器的最大分辨率.我们无法获得亚毫秒级 PIC 重新编程的效果是导致操作系统更频繁地唤醒.这增加了我们的机会应用程序将在我们的时间由操作系统通知指定的.我说,增加机会"是因为我们仍然不能保证我们实际上会收到通知,即使操作系统在我们告知后正常运行.

The MM Timer APIs allow the developer to reprogram the Programmable Interrupt Controller (PIC) on the machine. You can specify the new timer resolution. Typically, we will set this to 1 millisecond. This is the maximum resolution of the timer. We can’t get sub-millisecond accuracy. The effect of this reprogramming of the PIC is to cause the OS to wake up more often. This increases the chances that our application will be notified by the operating system at the time we specified. I say, "Increases the chances" because we still can’t guarantee that we will actually receive the notification even though the OS work up when we told it.

还有:

请记住,PIC 用于唤醒操作系统,以便它可以决定接下来应该运行什么线程.操作系统使用一些非常复杂的规则来确定接下来哪个线程占用处理器.其中两个操作系统查看以确定它是否应该运行线程或不是线程优先级和线程量子.

Remember that the PIC is used to wake up the OS so that it can decide what thread should be run next. The OS uses some very complex rules to determine what thread gets to occupy the processor next. Two of the things that the OS looks at to determine if it should run a thread or not are thread priority and thread quantum.

因此,即使您将分辨率降低到最大 1 毫秒,也不能保证您的线程会被选中执行其工作.

So, even if you put the resolution down to the maximum of 1ms, you are not guaranteed that your thread will be the one chosen to do its work.

这篇关于了解多媒体定时器的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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