Thread: Idling and not looping.

  1. #1
    0x01
    Join Date
    Sep 2001
    Posts
    88

    Idling and not looping.

    How does one idle a program? If I use a loop that keeps searching for data to act on, the CPU usage will
    be around 98% because the program sends instrutions to the processor over and over again non-stop.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    try
    while(1)
    _asm NOP

    nah, depends....sleep() maybe?
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  3. #3
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    For windows you would use Sleep(). There really is no standard way to do this. This is one occasion where system dependent solution is the better solution.
    Woop?

  4. #4
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    Depends on the OS, and also to a great extent on what kind of data. Mouse, keyboard, network?
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  5. #5
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Lightbulb Under the hood...

    In general, you can't "idle" a microprocessor. If your CPU usage is 10%, this means the other 90% of the CPU cycles are being used by the operating system in a (full-speed) do-nothing loop.

    On a PC, it is better to let the operating run the do-nothing loop so that your program doesn't "hog" the CPU. (You must use non-standard non-portable C++ to do this.) For example, Sleep() tells the operating system not to execute your program for awhile. If no other applications are waiting for CPU time, the operating system executes a do-nothing loop until something more important needs to be done.

    Most CPUs do have a "halt" state (and maybe a halt instruction), but this is generally used for start-up, shut-down, or reset. Some CPUs also have power-saving modes... AFAIK, the Intel processors slow-down, but don't fully-stop in power-save mode.
    Last edited by DougDbug; 04-15-2005 at 02:56 PM.

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    Sleep() isn't the same as NOP, the NOP instruction just takes up space and simulates lag because the processor still has to read through that segments offset (but it does nothing). Just so you know that they aren't the same thing.

    Sleep uses different things in windows, something about timers, I forgot..I wasn't paying attention very well in that lecture :/ Something about halting the process and "giving up its timeslot" so that other programs can use the timeslot allocated for that process.

    But as far as idling goes, it depends on what you want your stop condition to be (when you want it to stop idling), want it to be based on a timer? Want it to be on a key press? Want it to be when the mouse is moved? The power button pressed(lol), device put into a USB port? You have a ton of options luckily ^_^
    Last edited by Tronic; 04-15-2005 at 02:22 PM.
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

Popular pages Recent additions subscribe to a feed