Thread: Eficient wait function

  1. #1
    Your imaginary friend
    Join Date
    Jan 2010

    Eficient wait function

    How would i go about making an EFICIENT wait method, I am willing to download libraries.

    Hope someone can help!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    I have to admit, the idea makes me giggle. What, do you want to be able to do five minutes of waiting in only three minutes?

    The sleep function (which may have some extra letters at the front, depending on your system) will cede time back to the operating system, if it's not being used. You can run a program in the background, if it takes a while to run. Otherwise I have no idea what you could want.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    There's an FAQ on this, which suggests what you probably consider an inefficient sleeping mechanism. FAQ > Sleeping

    But in general this question is platform-specific.
    • For Linux: see the sleep() function, "man 3 sleep" (second accuracy), and the usleep() function (millisecond accuracy)
    • For Windows: try Sleep() from windows.h (millisecond accuracy)
    • For DOS (heaven forbid): delay()

    If you're using Boost you have access to platform-independent sleeping functions, but since you're asking this question I'm guessing you're not using Boost.

    Finally, you can get better than millisecond accuracy with functions like nanosleep() [Linux], but it's usually not worth it since the operating system will probably keep your process swapped out for coarser periods of time than this.

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell

    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ:

    My website:
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Jun 2005
    It's not only nanosleep() that is inaccurate. With windows and unix there is also jitter in the "1 second" or "1 millisecond" accuracy offered by Sleep()/sleep/usleep(). For example, sleeping for 1 millisecond can easily result in an actual pause exceeding 10 or 20 milliseconds due to context switching with other threads or processes.

    If you want a specified precision in the length of the pause, it is necessary to use an RTOS (real-time operating system) that is designed for such things and also select hardware appropriately (eg specialised timing circuits).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

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. Brand new to C need favor
    By dontknowc in forum C Programming
    Replies: 5
    Last Post: 09-21-2007, 10:08 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM