Thread: How can I "sleep" without hogging CPU

  1. #1
    Unregistered
    Guest

    How can I "sleep" without hogging CPU

    I wonder if someone would be kind enough to help me with a "timer" problem.
    Briefly, I want to start a program passing it a delay in seconds as an argument, and a batch file (or similar) to carry out at the elapsed time. (eg. prog 10 do_it.bat)
    I've got the following funtion in main:

    void snooze( clock_t wait ) {
    clock_t targ;
    targ = wait + clock();
    while( targ > clock() ) {
    /* wait ! */
    }
    }

    which is called thus:
    snooze((clock_t)nSecs * CLOCKS_PER_SEC); (where nSecs is argv[1])

    and everything's lovely....BUT then I tried to do something else on the computer whilst running it and my CPU's 98.9% used !!

    Can anyone suggest a better way of doing this please - there's no real urgency, it's just something I wanted to do 'cos I figured it would be interesting !

  2. #2
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    522
    Have you tried the _sleep() function?
    Code:
    _sleep(1000); // sleeps for 1 second
    Takes the number of milliseconds as its argument.
    Can be found in <stdlib.h>
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    43
    The sleep() dosen't work. I have looked in the FAQ and have this working:

    Code:
    void sleep_seconds ( long seconds ) {
      clock_t limit, now = clock();
      limit = now + seconds * CLOCKS_PER_SEC;
      while ( limit > now )
        now = clock();
    }
    but this hogs the CPU. What commands work with the Pelles C for Windows, that create a delay without using up all the CPU?

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    I believe the sleep() function in windows.h uses the spare time to give to other processes.

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    43
    I get a "Missing Prototype for 'Sleep'" error

    Code:
    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
    #include <windows.h>
    
    int main (void){
    	
    printf("1");
    
    sleep(2000);
    
    printf("2");
    sleep(3000); 
    printf("3");
    
    return 0;
    }

  6. #6
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    I'm not closing this thread, but in the future, please don't bump extremely old threads like this.

  7. #7
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    Sleep() with a capital "S".
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    My fault, I apologize. Same problem earlier this week!

  9. #9
    Registered User
    Join Date
    Dec 2004
    Posts
    43
    Thanks guys. It's working now

  10. #10
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    in MSVC sleep()(the C Standard stdlib.h version) is _sleep(), they add underscores to stuff the consider legacy or something stupid like that... i dont remember but its stupid...

    edit: then agian i could be wrong? i dont use MSVC anymore.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  11. #11
    Quote Originally Posted by no-one
    in MSVC sleep()(the C Standard stdlib.h version) is _sleep()
    sleep() (whatever the details) is not standard.
    Emmanuel Delahaye

    "C is a sharp tool"

  12. #12
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    haha thats right...
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  13. #13
    Registered User
    Join Date
    Mar 2004
    Posts
    16
    Quote Originally Posted by Emmanuel Delaha
    sleep() (whatever the details) is not standard.
    It depends what you mean by "standard". It isn't ANSI, but it conforms to POSIX standard...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pthread question
    By quantt in forum Linux Programming
    Replies: 7
    Last Post: 04-07-2009, 01:21 AM
  2. questions on multiple thread programming
    By lehe in forum C Programming
    Replies: 11
    Last Post: 03-27-2009, 07:44 AM
  3. Upgrading my old CPU (for another old one!)
    By foxman in forum Tech Board
    Replies: 16
    Last Post: 01-11-2008, 05:41 PM
  4. Can you still view the bios screen with a bad CPU?
    By HyperCreep in forum Tech Board
    Replies: 4
    Last Post: 12-31-2006, 06:57 PM
  5. CPU temp
    By PING in forum Tech Board
    Replies: 5
    Last Post: 01-28-2006, 06:25 AM