www.delorie.com/djgpp/v2faq/faq22_27.html   search  
Node:usleep, Next:, Previous:Reboot the PC, Up:Miscellany

22.27 Delaying execution for short periods of time

Q: The function usleep doesn't work for me! When its argument is less than 10000, there's no delay at all, and when the argument is larger than 10000, the delay is always the same....

Q: How can I delay the execution of my program for 2msec?

Q: I need to pause my program for 100 microseconds. Can I do it?

A: Most time-related facilities in DJGPP have the same 55msec granularity of the time intervals they measure. This is because the timer tick interrupt that updates the time has the frequency of 18.2Hz. This is why calling usleep with arguments less than 55000 produces strange effects: the resolution of the argument is 1usec (for compatibility with other compilers), but the granularity is still 55msec.

If you need to pause your program for periods of time shorter than 55msec, you have several alternatives:

  1. For delays longer than 1msec, use the library function delay. It is based on the CMOS clock chip whose frequency is 1024Hz.
  2. For periods shorter than 1msec, write your own wait loop that calls library function uclock to see when the pause time expires. uclock measures time with 840-nanosecond granularity. Here's a possible implementation of such a wait loop:
     #include <time.h>
    
     uclock_t start;
    
     /* Wait for 200 microseconds.  */
     start = uclock ();
     while (uclock () < start + UCLOCKS_PER_SEC / 5000)
       ;
    



  webmaster   donations   bookstore     delorie software   privacy  
  Copyright 2001   by Eli Zaretskii     Updated Apr 2001