Delay for ms - CPU Usage

This is a discussion on Delay for ms - CPU Usage within the C++ Programming forums, part of the General Programming Boards category; Hi! If I delay my program for certain amount of milliseconds, how much CPU is used?...

  1. #1
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669

    Delay for ms - CPU Usage

    Hi!

    If I delay my program for certain amount of milliseconds, how much CPU is used?
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    Depends on what you use. If you use Sleep() I believe it doesn't waste CPU cycles(This is for windows). If you use a standard solution it will basically put your program in a loop which will eat some cycles. This would be one of those times where system dependent is better than standard.
    Woop?

  3. #3
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Sleep() makes the CPU time avaliable to other applications.

    If your program is looping for a long time, you should include Sleep() unless your application really needs to utilize all of the CPU power available. I've used a few applications that allow the user to select "CPU priority" or "CPU utilization". I assume this allows me to change the Sleep() time.

    It is bad practice (with a multitasking OS) to hog the CPU if your program is running a loop that's not doing anything useful while you wait for something else to happen.

    Your CPU is actually running 100% all the time... But during the sleep-time, your code is not executing.

    If Windows says that your CPU usage is 5%, then 95% of the cycles are being "wasted" by the operating system running a do-nothing loop... waiting for something exciting to do!

  4. #4
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    heh, the OS will not just keep your application using the CPU exclusively until you sleep. If another process (for instance, explorer.exe) needs CPU time, it will use context switching and allow the other process some cycles and then return to yours. Don't get the impression that you have complete control of the CPU at any time. If you don't need to loop constantly, then don't, if you do, then let your process do the work and if it's not as high a priority, then use the API for handling thread priorities.

  5. #5
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Rockytriton is right! The operating system will allocate time to ALL running applications that request it. With Windows, the only way to "take over" the CPU, is with a kernel-mode program (driver).

    But, the operating system doesn't know if your program is really doing anything useful, or just running a "useless" loop. So, it can slow-down other applications... Well, it can slow-down other applications even if it's running a useful loop.

    Wow! Rockytriton, you're really in Antarctica? That's coo.... umm.. I mean awesome!

  6. #6
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    I would suggest if you aren't doing something useful in a loop that instead of calling Sleep(), you call break;

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Quote Originally Posted by DougDbug
    With Windows, the only way to "take over" the CPU, is with a kernel-mode program (driver).
    You could set any process to REALTIME_PRIORITY_CLASS with SetPriorityClass and your process would get higher priority then the OS but that would't be a good idea.
    Sleep() isn't accurate. The millisecond value passed to it should just be seen as a sugested sleep time.
    Last edited by Quantum1024; 10-27-2005 at 09:58 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reduce CPU usage
    By patrick22 in forum Windows Programming
    Replies: 9
    Last Post: 07-10-2009, 02:13 PM
  2. questions on multiple thread programming
    By lehe in forum C Programming
    Replies: 11
    Last Post: 03-27-2009, 07:44 AM
  3. Net cpu usage of pthreads?!
    By mynickmynick in forum C++ Programming
    Replies: 15
    Last Post: 09-26-2008, 07:59 AM
  4. Calculating CPU Usage
    By vitaliy in forum Linux Programming
    Replies: 3
    Last Post: 08-21-2005, 09:38 AM
  5. CPU Usage so high
    By X PaYnE X in forum Windows Programming
    Replies: 9
    Last Post: 12-21-2003, 02:07 AM

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