sleep

This is a discussion on sleep within the C++ Programming forums, part of the General Programming Boards category; My last thread got deleted and I know I shouldnt make a new one. But I cant get the "sleep" ...

  1. #1
    Pawn, Pascal and C++
    Join Date
    Sep 2005
    Posts
    90

    Unhappy sleep

    My last thread got deleted and I know I shouldnt make a new one.
    But I cant get the "sleep" function to work.
    I have "time.h" included.
    And then I have "sleep(1);" which is supposed to make the program stop in one sec right?
    But when I compile it says:
    implict declarion of function `int sleep(...)'
    (I dont have int in front of sleep)

    Yes, I've searched....

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by XunTric
    And then I have "sleep(1);" which is supposed to make the program stop in one sec right?
    No. It the amount of time the program remains stopped before continuing.

    Quote Originally Posted by XunTric
    But when I compile it says:
    implict declarion of function `int sleep(...)'
    (I dont have int in front of sleep)
    The error means you didn't include the right header file. man pages will probably tell you which one to include.
    Last edited by Ancient Dragon; 09-26-2005 at 12:16 PM.

  3. #3
    Pawn, Pascal and C++
    Join Date
    Sep 2005
    Posts
    90
    Pages?
    I've searched and found time.h...

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    On windows it's Sleep, and the parameter is milliseconds, so 1000 = 1 second. It is declared in <Windows.h>.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    *nix its sleep(int seconds) (not to be confused with the Windows Sleep() function that David described)

  6. #6
    Pawn, Pascal and C++
    Join Date
    Sep 2005
    Posts
    90
    Well if its not the windows (as I tryed too without luck) what to have then Dragon?

    EDIT:
    Well the windows sleep would be better actually because I want a little shorter than a second.
    But that aint working either.
    Last edited by XunTric; 09-26-2005 at 01:20 PM.

  7. #7
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    You can always create your own (blocking) sleep function by using a simple while loop and the functions found in the <ctime>, specifically clock(), and the macro CLOCKS_PER_SEC.

  8. #8
    Pawn, Pascal and C++
    Join Date
    Sep 2005
    Posts
    90
    Quote Originally Posted by Speedy5
    You can always create your own (blocking) sleep function by using a simple while loop and the functions found in the <ctime>, specifically clock(), and the macro CLOCKS_PER_SEC.
    All I found in ctime was this...
    Code:
    // The -*- C++ -*- time header.
    // This file is part of the GNU ANSI C++ Library.
    
    #ifndef __CTIME__
    #define __CTIME__
    #include <time.h>
    #endif
    And then im getting to the time.h again that isnt working.

  9. #9
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by XunTric
    Well if its not the windows (as I tryed too without luck) what to have then Dragon?

    EDIT:
    Well the windows sleep would be better actually because I want a little shorter than a second.
    But that aint working either.
    Which version you use depends on which operating system you are using. Sleep() is only supported on MS-Windows, while sleep() is for *nix. On MS-Windows, gc++ compiler may also support *nix version of sleep.

    Like David mentioned, if you are using MS-windows, then you need to pass 1000*60 (1000 milleseconds in a second, and 60 seconds in a minute).
    Code:
    // sleep 1 minute
    Sleep(1000*60);
    
    // sleep 1 second
    Sleep(1000);
    you will never ever get the os to put your program to sleep for an exact number of milliseconds because neither MS-Windows nor *nix are real-time operating systems. About 250 ms is the shortest possible time.
    Last edited by Ancient Dragon; 09-26-2005 at 01:40 PM.

  10. #10
    Pawn, Pascal and C++
    Join Date
    Sep 2005
    Posts
    90
    Oh... Well I got Windows XP
    And I use Dev-C++

    So could you help me with that thing in ctime, because I got no idea on how to do that or simply send me a working module for windows?

  11. #11
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Code:
    #include <windows.h>
    
    int main()
    {
       // put program to sleep for about 1 second
       Sleep(1000);
    
       return 0;
    }

  12. #12
    Pawn, Pascal and C++
    Join Date
    Sep 2005
    Posts
    90
    Then i get the same error as in the first post.
    I searched windows.h and there is actually no sleep function there.

  13. #13
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    windows.h includes a lot of other header files. see Winbase.h. That program compiles ok with my version (4.9.9.2) of Dev-C++

  14. #14
    Pawn, Pascal and C++
    Join Date
    Sep 2005
    Posts
    90
    Ok, now I've updated my Dev-C++ too, but without luck =(
    Got a new error now tho:

    In function `int main()':
    `sleep' is undeclared(first use in this function)
    (Each undeclared identifier is reported only once for each function it appears in.)

    Got any ideas?
    Thanks for ur help to now!

    EDIT:

    Actually, the example code you gave me compiles fine.
    But my code doesnt...

    I have it like this:
    Code:
    int num1 = 0;
    
    while(num1 < 101) {
    
      cout<< num1 << "%";
      num1 += 10;
      sleep(1000);
    }
    (I know that will print "%" a hundred times. Ill fix that later :P)
    Last edited by XunTric; 09-26-2005 at 02:25 PM.

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> Then i get the same error as in the first post.

    The error in the first post mentions sleep with a lower-case 's'. Make sure you are using Sleep with an upper-case 'S'. Case matters in C++.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [pthread] cancellable sleep in win32?
    By cyberfish in forum C++ Programming
    Replies: 2
    Last Post: 08-11-2007, 03:30 AM
  2. Sleep works with just one thread, but not 2
    By finkus in forum C++ Programming
    Replies: 5
    Last Post: 12-01-2005, 09:17 PM
  3. Problem with Sleep() #$@^#$%^
    By intruder in forum C++ Programming
    Replies: 8
    Last Post: 10-11-2004, 07:46 AM
  4. why do we require sleep?
    By jinx in forum A Brief History of Cprogramming.com
    Replies: 43
    Last Post: 07-14-2004, 09:21 AM
  5. Sleep is overrated...
    By Polymorphic OOP in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 01-24-2003, 12:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21