如何赋予此时间紧迫的过程优先于其他线程的作用? [英] How can I give this time-critical procedure priority over other threads?

查看:87
本文介绍了如何赋予此时间紧迫的过程优先于其他线程的作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了编写MIDI音序器,我需要一个稳定的脉冲来调用定时例程,该例程具有绝对优先级,高于程序中的任何其他内容,并且优先于计算机中的任何其他内容。我通过使用这样的TimeSetEvent来做到这一点:

In order to write a MIDI sequencer I need a steady pulse that calls a timing routine that has absolute priority over anything else in the program and preferrably over anything in the computer. I do this by using TimeSetEvent like this:

TimeSetEvent (FInterval, 0, TimerUpdate, uInt32 (Self), TIME_PERIODIC);

其中,TimerUpdate是一个回调,该回调以优先级tpTimeCritical恢复一个单独的线程,并且其中一个调用例程(FOnTimer )来处理所有MIDI事件。

where TimerUpdate is a callback that resumes a separate thread with priority tpTimeCritical and that one calls a routine (FOnTimer) in which to handle all MIDI events.

procedure TThreaded_Timer.Execute;
begin
   if Assigned (FOnTimer) then
   begin
      while not Terminated do
      begin
         FOnTimer (Self);
         if not Terminated then Suspend;
      end; // while
   end; // if
   Terminate;
end; // Execute //

尽管这种构造比我之前尝试过的某些东西要好得多对某些事件敏感。令我惊讶的是,它在每次显示提示时都会口吃。为什么简单的提示会导致这样的时间紧迫线程中断?当然可以关闭它,但是还有哪些令人讨厌的惊喜还在等着我?

Although this construction is much better than some things I tried before it is still very sensitive to some events. To my surprise it stutters at each display of a hint. Why can a simple hint cause such an interruption of a time critical thread? Of course I can switch it off but which nasty surprises are still waiting for me?

推荐答案

多媒体计时器的准确性不是此处是解释原因的文章。

The accuracy of the multimedia timers is not that great.Here is an article that explains why.

而不是依靠计时器来唤醒您的线程为什么不管理线程本身内的睡眠和唤醒时间?

Instead of relying on a timer to wake up your thread why don't you manage your sleep and wake times all within the thread itself?

也许是这样的(用伪代码,抱歉,我不这样做) t知道德尔菲):

Maybe something like this (in pseudo-code, sorry I don't know Delphi):

my_critical_thread()
{
    while (true) {
        time = get_current_time()
        do_work();
        time = interval - (get_current_time() - time)
        if (time > 0)
            sleep(time)
    }
}

如果线程设置为关键优先级,这将使您非常接近目标间隔,并假设您在每次迭代中都进行了工作为其他线程和系统其余部分留出时间来做它们的事情。

This should get you very close to your target interval if the thread is set to critical priority, assuming the work you do on each iteration leaves time for your other threads and the rest of the system to do their thing.

祝你好运。

这篇关于如何赋予此时间紧迫的过程优先于其他线程的作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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