Thread: Create a delay function

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    66

    Create a delay function

    Is there a way to create an accurate delay() function without hogging the CPU?

    I tried this, it's accurate, but it still hogs the CPU.

    Code:
    void delay(int a)
    {
    
    clock_t sec;
    sec = clock();
    while((clock() - sec) < a);
    
    }

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    depending on the OS - yes.
    For example in windows WaitForSingleObject with the handle of the curent thread will give the very exact result
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    In your Help >> Index, check out delay() and sleep().

    If you really want something else done by the cpu in the meantime, you may need to do something like this:

    delay(1/4th of total delay you want)
    call function of code you want executed
    delay(1/4th of total delay you want)
    call another function/same function as before to execute some code, there.
    delay(1/4th of total delay you want)
    etc.

    Or same idea with sleep().

    I've never done it with the Windows API, but it might be possible that way, as well.

    Have you checked the Windows forum? Might be even in their FAQ.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    You can also check out Sleep() which is Windows-specific (not the same as sleep). I believe you need winuser.h to use it.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    All Win32 API users should include Windows.h and not winuser.h.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    You can also use the select() function for extra portability.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Replies: 11
    Last Post: 03-24-2006, 11:26 AM
  4. Creating a student grade book-how?
    By Hopelessly confused in forum C Programming
    Replies: 5
    Last Post: 10-03-2002, 08:43 PM
  5. Replies: 4
    Last Post: 11-07-2001, 02:46 PM