正文

WINDOWS环境下的精确延时类2005-12-25 10:50:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/zz80/8801.html

分享到:

这是转自国外的一篇文章,实现了在WINDOWS下精确延时的功能


The proposed solution uses critical sections that don't rely on the thread scheduler to perform time slice allocation and thus are enterable as soon as they become available. This solution utilises the multimedia timer's capabilities to create periodic timers with the highest resolution possible (1msec) and a critical section to provide switching between the threads.

Wait happens because the main program thread gets blocked on the timer critical section while the timer performs counting down cycles. Once this happens, the timer thread leaves the critical section, thus allowing the main thread to continue. Once the main thread enters the timer's critical section, it leaves it immediately, thus allowing the timer thread to block it again on the next pass.

Because this solution does not involve context switching, the delay is minimal. The error level of this timer is below 5%. The presented class can be utilised as shown in the Delay() method below. This example uses high-resolution timers to calculate the actual delay and average delay error level. One can call this method n-number of times with different delay values to verify the error level of this timer.

Enjoy!

//----------------------------------------------------------------
class PreciseTimer
{
public:
   PreciseTimer() : mRes(0), toLeave(false), stopCounter(-1)
   {
      InitializeCriticalSection(&crit);
      mRes = timeSetEvent(1, 0, &TimerProc, (DWORD)this,
                          TIME_PERIODIC);
   }
   virtual ~PreciseTimer()
   {
      mRes = timeKillEvent(mRes);
      DeleteCriticalSection(&crit);
   }

   ///////////////////////////////////////////////////////////////
   // Function name   : Wait
   // Description     : Waits for the required duration of msecs.
   //                 : Timer resolution is precisely 1 msec
   // Return type     : void  :
   // Argument        : int timeout : timeout in msecs
   ///////////////////////////////////////////////////////////////
   void Wait(int timeout)
   {
      if ( timeout )
      {
         stopCounter = timeout;
         toLeave = true;
         // this will do the actual delay - timer callback shares
         // same crit section
         EnterCriticalSection(&crit);
         LeaveCriticalSection(&crit);
      }
   }
   ///////////////////////////////////////////////////////////////
   // Function name   : TimerProc
   // Description     : Timer callback procedure that is called
   //                 : every 1msec
   //                 : by high resolution media timers
   // Return type     : void CALLBACK  :
   // Argument        : UINT uiID :
   // Argument        : UINT uiMsg :
   // Argument        : DWORD dwUser :
   // Argument        : DWORD dw1 :
   // Argument        : DWORD dw2 :
   ///////////////////////////////////////////////////////////////
   static void CALLBACK TimerProc(UINT uiID, UINT uiMsg, DWORD
                                  dwUser, DWORD dw1, DWORD dw2)
   {
      static volatile bool entered = false;

      PreciseTimer* pThis = (PreciseTimer*)dwUser;
      if ( pThis )
      {
         if ( !entered && !pThis->toLeave )   // block section as
                                              // soon as we can
         {
            entered = true;
            EnterCriticalSection(&pThis->crit);
         }
         else if ( pThis->toLeave && pThis->stopCounter == 0 )
                                              // leave section
                                              // when counter
                                              // has expired
         {
            pThis->toLeave = false;
            entered = false;
            LeaveCriticalSection(&pThis->crit);
         }
         else if ( pThis->stopCounter > 0 )   // if counter is set
                                              // to anything, then
                                              // continue to drop
                                              // it...
            --pThis->stopCounter;
      }
   }

private:
   MMRESULT         mRes;
   CRITICAL_SECTION crit;
   volatile bool    toLeave;
   volatile int     stopCounter;
};

//----------------------------------------------------------------
// Class usage example
//----------------------------------------------------------------
void Delay(unsigned int val)
{
   static LARGE_INTEGER freq = {0};
   static double average = 0;
   static int count = 0;

   ++count;
   LARGE_INTEGER iStart, iStop;
   if ( freq.QuadPart == 0 )
      QueryPerformanceFrequency(&freq), freq.QuadPart /= 1000;
                      // convert to msecs counter

   double sleep = 0;
   QueryPerformanceCounter(&iStart);

   timer.Wait(val); // is there anything to wait on? ... then wait

   QueryPerformanceCounter(&iStop);
   sleep = ((double)iStop.QuadPart - (double)iStart.QuadPart)
                                   / (double)freq.QuadPart;
   average += (val ? 100.0*(sleep-val)/(double)val : 0);
   printf("Waited for %6.3f (%ld). error = %5.2f\n", sleep, val,
                                   average/(double)count);
}

 

阅读(4381) | 评论(2)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

loading...
您需要登录后才能评论,请 登录 或者 注册