Creating a High-Precision, High-Resolution, and Highly Reliable Timer, Utilising Minimal CPU Resources

Environment: Windows NT/2K/XP, MSVC 6

While the Microsoft Win32 API provides functions for dealing with waitable timer objects that provide clients with very high resolution (100 nSec.), it gives no guarantee as to the actual precision of those timers. Because those timers rely solely on the APC mechanism to deliver notification to the timer callbacks and those callbacks can only be invoked when threads are put into an APC alertable state, precision is very, very poor. This is because it relies on the thread scheduler, which usually preforms thread context switching every 10msecs delays. Unfortunately, this is not easily overcome. To a lesser extent, the same problem exists when using high-precision multimedia timers.

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 imer'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);
}


Comments

  • Xamvl Rnw ubki

    Posted by KUvasyYIGy on 07/19/2013 11:47pm

    xanax generic buy xanax online legitimate - much 2mg xanax worth

    Reply
  • RawteR yj JQ wuo vVtO Bp

    Posted by LwSBHtzYZY on 06/19/2013 09:33pm

    buy tramadol online tramadol acetaminophen high - ordering tramadol online legal

    Reply
  • The ultimate plan for the shoes that you may discover more about straight away.

    Posted by BobHotgloff on 05/22/2013 01:52am

    The Techniques To Gain knowledge of shoes And How One Might Link up with The sneakers Top dogs [url=http://www.shoesja.biz/]ベルーナ[/url] Machines and fabrication throughout Big Apple - shoes will leave with no good-bye [url=http://www.shoesja.biz/adidas-アディダス-c-64.html]adidas アディダス[/url] Some shoes Venture Call - - The Ones Who likes pretty much nothing revenues? [url=http://www.shoesja.biz/new-balance-ニューバランス-c-21.html]ニューバランス キッズ[/url] Recent questions about sneakers have been answered not to mention the reason why you have to look into every single statement of this specific guide. [url=http://www.shoesja.biz/nike-ナイキ-c-44.html]ナイキスニーカー[/url] The best way to find out every single thing there is to know about shoes in nine basic steps. [url=http://www.kutuja.com/]アシックス[/url] Ways to fully understand anything there is to understand surrounding shoes in A few relatively easy steps. [url=http://www.kutuja.com/adidas【アディダス】-c-1.html]adidas アディダス[/url] Creative queries about sneakers replied not to mention why you need to look into every single term of this specific e book. [url=http://www.kutuja.com/new-balance【ニューバランス】-c-206.html]ニューバランス 574[/url] Further from the ultimate plan for sneakers that you simply could explore immediately. [url=http://www.kutuja.com/nike【ナイキ】-c-215.html]ナイキ スニーカー[/url] An additional double turn on sneakers [url=http://www.shoesjp.biz/]アディダス[/url] Fascination Formula For shoes [url=http://www.shoesjp.biz/adidas【アディダス】-c-640.html]adidas[/url] An important double twist on shoes

    Reply
  • Exactly what everyone else actually does regarding nike and the thing you will need to try and do totally different.

    Posted by icoppyapedcap on 04/21/2013 09:00am

    Bo [url=http://hunter-rain-boots.webnode.jp]レインブーツハンター[/url] uSd [url=http://hunterrainbootsjp.webnode.jp]ハンターブーツ[/url] b ZhhSfa ZkpJ [url=http://hunter-boots8.webnode.jp]レインブーツメンズ[/url] tr E [url=http://rain-boots-men.webnode.jp]ブーツ[/url] gh [url=http://hunter-rain-boots-ja.webnode.jp]レインブーツメンズ[/url] JczVjvXzz C[url=http://rainshoesja.webnode.jp]ブーツ[/url] apCfzSbrAa [url=http://ja-hunter-rain-boots.webnode.jp]レインブーツハンター[/url] p MppRqp [url=http://rain-boots-popular.webnode.jp]レインシューズ[/url] Clb [url=http://rain-boots-men6.webnode.jp]hunter レインブーツ[/url] Rcp Enm [url=http://jahunterrainboots.webnode.jp]ハンター長靴[/url] Gnm

    Reply
  • How to put the thread in sleep mode for less than 1 milisecond in windows ,vb.net

    Posted by manish garg on 07/20/2012 04:57am

    i need to do the same but i dont know c++ , my application is written in vb.net. I would like my thread to go in for a sleep for say about 100 microsecond (.1 milisecond). I dont know how to do that. Can you help Thanks in anticipation.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • A help desk is critical to the operations of an IT services business. As a centralized intake location for technical issues, it allows for a responsive and timely solution to get clients and their staff back to business as usual. In addition to handling immediate IT issues, a help desk performs several proactive tasks to ensure clients' IT systems remain operational and downtime is minimized. Thus, utilizing a help desk and following best practices can improve the productivity, efficiency and satisfaction of …

  • With JRebel, developers get to see their code changes immediately, fine-tune their code with incremental changes, debug, explore and deploy their code with ease (both locally and remotely), and ultimately spend more time coding instead of waiting for the dreaded application redeploy to finish. Every time a developer tests a code change it takes minutes to build and deploy the application. JRebel keeps the app server running at all times, so testing is instantaneous and interactive.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds