Application Modernization: What Is It and How to Get Started

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.


class PreciseTimer
   PreciseTimer() : mRes(0), toLeave(false), stopCounter(-1)
      mRes = timeSetEvent(1, 0, &TimerProc, (DWORD)this,
   virtual ~PreciseTimer()
      mRes = timeKillEvent(mRes);
   // 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
   // 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;
         else if ( pThis->toLeave && pThis->stopCounter == 0 )
                                              // leave section
                                              // when counter
                                              // has expired
            pThis->toLeave = false;
            entered = false;
         else if ( pThis->stopCounter > 0 )   // if counter is set
                                              // to anything, then
                                              // continue to drop
                                              // it...
   MMRESULT         mRes;
   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;

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

   double sleep = 0;

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

   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,


  • software engineer

    Posted by Alan Kurlansky on 06/18/2014 04:52pm

    I am able to build your class and run it, but it generally exits immediately with this result: Waited for 0.000 . error = -100.00 Not clear to me how the main program section blocks while the timer counts down. In other words how do I set that up? My goal is to create an endless while loop that sleeps for x microseconds with each iteration. What am I missing? Thanks in advance for any help with this.

    • Long overdue, but for completeness...

      Posted by Steve Le Roy Harris on 02/16/2017 07:06pm

      The example doesn't work because PreciseTimer::TimerProc is called for the first time and sets up its variables and critical section lock 1ms after PreciseTimer is created. If timer.Wait is called less than 1ms after PreciseTimer is created it returns immediately because there was no lock. Adding Sleep(2) to PreciseTimer's constructor ensures TimerProc has run at least once and should work.

  • 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 11: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.

  • You must have javascript enabled in order to post comments.

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

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date