High-Precision Timing Under Windows, Windows NT, & Windows 95

Last reviewed: May 3, 1996
Article ID: Q148404
The information in this article applies to:
  • Microsoft Windows version 3.1
  • Microsoft Win32 Software Development Kit (SDK) for:

        - Windows NT versions 3.5 and 3.51
        - Windows 95
    

SUMMARY

Windows 3.1 developers who require high precision timers can use the multimedia timers, which are accurate to 10 milliseconds (ms) in most cases. However, in the multithreaded environments of Windows NT and Windows 95, the resolution and accuracy of the multimedia timers may often be reduced due to the preemptive nature of the operating systems.

MORE INFORMATION

In Windows 3.1, multimedia timers are serviced by an interrupt service routine (ISR) which is tied to the computer's clock. When a tick on the clock occurs, Windows suspends the running program and services the tick. Should your application use multimedia timers (by using TimeSetEvent() and any of the CALLBACK flags), Windows executes your callback function before returning control to the suspended program. Servicing your callback function during the hardware timer tick provides high precision timing because your callback is being executed within the timer interrupt, and interrupt routines cannot, themselves, be interrupted.

The multithreaded environments of Windows NT and Windows 95 employ CPU scheduling techniques to improve processor utilization and smooth multitasking. The portions of code, called threads, within the system are organized into queues and assigned priorities. Threads of code required by the operating system are assigned high priorities. The thread scheduler allows a thread a predetermined amount of execution time (quantum). Then it evicts the thread from the processor until all threads at the specific priority level have been serviced. The process continues at the next, lower, priority level. The timer-servicing thread, like applications, is subject to CPU scheduling and eviction regardless of the thread priority. Given that the number of threads ready for execution changes frequently, the period between timer thread executions can vary dramatically.

For example, say five time-critical threads are ready for execution, and the quantum is q milliseconds. If the timer thread is fifth, four threads execute before the timer thread is executed; q milliseconds x 4. This means that if q is 25 milliseconds, 100 milliseconds will have elapsed before the fifth thread is executed. Boosting the priority of the thread that created the timer risks introducing instability in the system by starving the time-sensitive, system-critical threads.


Additional reference words: 4.00 3.50 multimedia resolution flat generic
KBCategory: kbmm kbprg kbhowto
KBSubcategory: MMTimer



THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: May 3, 1996
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.