Thread: sleep function

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    83

    sleep function

    Hi,
    Why would gcc return, "warning: implicit declaration of function 'sleep'"
    ?
    I do have time.h included.


    Thanks for your help

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Because sleep isn't in time.h? It's in unistd.h, on *nix machines.

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    83
    Is there anything I can use to make a timer... or basically just a delay without that header?

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by pollypocket4eva View Post
    Is there anything I can use to make a timer... or basically just a delay without that header?
    A long running loop? But that would also eat up a lot of CPU time, and it would wait for a different amount of time on computers with a different CPU speed,
    Why wouldn't you want to use sleep()?
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    As cpjust says, I would like to understand why you want to sleep, and yet you do not want to include the correct header-file to achieve that. Either you need to sleep, and thus need to include the correct headerfile (whatever that may be), or you don't call sleep.

    Loops can be very tricky for this, as if you want a delay that works the same on a 500MHz P3 with compiled with low optimization level and a 4GHz P4 with compiled with high optimization settings, you need some pretty clever programming. Not to mention than many compilers will optimize empty loops into "nothing".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    And how would not including a header be an improvement?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Salem View Post
    And how would not including a header be an improvement?

    My guess is that this is misdirected attempt to "fancy up" a program, but the students are given a strict list of header files. So the student is trying to solve the problem in a way that doesn't involve including some header that is on the "white-list", and still get the desired delaying effect. I say misdirected, because for the most cases, delays/sleeps are completely meaningless to average users, and annoying to advanced users.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    There are some practical applications for pauses or delays. I am currently writing software that runs diagnostics on a voltage reader. The reader needs to wait approximately 5 seconds before reading the first voltage to allow for any transients to pass.

  10. #10
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    Speaking of which, I had planned to use a loop to compare start time to current time (using time() from time.h). However this continually uses the CPU, which is something I would like to avoid. I would use the sleep functions from windows.h but I'm running the software I'm writing on a DOS only machine... Is there a sleep equivalent that would work in DOS?

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Yes, there is. It's <you'll love this!> called:

    Code:
    sleep(N);  //where you want N seconds (or what DOS will report as N seconds), of inactivity
    The include file is dos.h

    That was easy!!

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    In a single-tasking OS, there is obviously nothing other than looping that will do this job, and as suggested, there is a sleep/delay function that will sleep for a set amount of time.

    I'm fully aware of the need to delay or stop an application for some time. However, I also know that there are LOTS of programmers who use this sort of thing unnecessarily in their programs when displaying messages etc. - the delay is often too short for new users, and gets annoying when it takes too long for experienced users. Waiting for the user to press a key is a much better approach in this sort of usage.

    Finally, perhaps one of the moderator team could split the new posts on this topic from the old thread.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

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. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  3. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  4. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM