do {} 13% CPU usage ?!?!?!?

This is a discussion on do {} 13% CPU usage ?!?!?!? within the C++ Programming forums, part of the General Programming Boards category; my PC uses 13% of CPU why what does PC while this??? Code: #include <iostream> using namespace std; int main ...

  1. #1
    beginner for now
    Join Date
    Oct 2009
    Posts
    35

    Angry do {} 13% CPU usage ?!?!?!?

    my PC uses 13% of CPU

    why what does PC while this???


    Code:
    #include <iostream>
    
    using namespace std;
    
    int main ()
    {
        bool game_main_menu = true;
        do
        {}
        while ( game_main_menu == true );
        int z;
        cin >> z;
        return 0;
    }

    I don't get it

    when I do that he is out of loop it goes to 0% CPU

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main ()
    {
        bool game_main_menu = true;
        do
        {}
        while ( game_main_menu == false );
        int z;
        cin >> z;
        return 0;
    }

    why I don't get it

    I think this is question for experts

    PC should do nothing if there is do {}

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,263
    Quote Originally Posted by military genius View Post
    my PC uses 13% of CPU
    PC should do nothing if there is do {}
    It IS "doing nothing". As fast as possible.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    beginner for now
    Join Date
    Oct 2009
    Posts
    35
    if it would do nothing it wouldn't use neighter 1% of CPU


    I wont to know what exactly it does

  4. #4
    Registered User jdragyn's Avatar
    Join Date
    Sep 2009
    Posts
    93
    You don't think game_main_menu == true is doing something?

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    It's comparing your variable to true, and then returning to the earlier part of the code. Over and over and over again.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Quote Originally Posted by military genius View Post
    if it would do nothing it wouldn't use neighter 1% of CPU


    I wont to know what exactly it does
    Here's what it does:

    Code:
    mov     ax, 1
    do_loop:
    cmp     ax, 0
    jne     do_loop
    It just executes the instructions cmp and jne over and over until the application is killed. Of course the actual instructions depends on the compiler, what optimizations are set, and the CPU the application is running on, but you should get the picture.
    bit∙hub [bit-huhb] n. A source and destination for information.

  7. #7
    beginner for now
    Join Date
    Oct 2009
    Posts
    35

    Thumbs up

    of corse

    yea thanks

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    Since the cpu does not need for any memory or disk access, it is pretty much just storming through the instructions, using up 100% of its resources executing the code.
    Which is exactly why using loops for these purposes is Not a Good Idea™.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Quote Originally Posted by military genius View Post
    if it would do nothing it wouldn't use neighter 1% of CPU


    I wont to know what exactly it does
    It's not doing nothing, it is checking if the condition is true or false, repeatedly, as fast as possible.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  10. #10
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    It looks to me as if that code would suck up 100% of at least 1 core depending on how the instructions were shared among the cores.

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

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