"PeekMessage()" - 50% of CPU...

This is a discussion on "PeekMessage()" - 50% of CPU... within the Windows Programming forums, part of the Platform Specific Boards category; Hi all, I have a little problem (in Windows programming). I was using "GetMessage()" function for message loop but now ...

  1. #1
    Beginning game programmer Petike's Avatar
    Join Date
    Jan 2008
    Posts
    64

    Question "PeekMessage()" - 50% of CPU...

    Hi all,
    I have a little problem (in Windows programming).
    I was using "GetMessage()" function for message loop but now when I changed to "PeekMessage()" loop, it occupies 50% of my CPU during the whole program.
    Here is the sample code:
    Code:
    while (TRUE)
    {
        if (PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE) == TRUE)
        {
            // If the message is WM_QUIT, exit the while loop
            if (Msg.message == WM_QUIT)
                break;
    
            // Translate and dispatch the message
            TranslateMessage(&Msg);
            DispatchMessage(&Msg);
        }
    }
    After running it, the program takes 50% of my CPU. But when I was using the "GetMessage()" function before, it was OK (at idle, 0% of CPU).
    Does anybody know where is the problem?

    Thanks.

    Petike

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Because PeekMessage tries to grab a message, if there is none, it returns immediately. GetMessage, on the other hand, will schedule another task when there is no message available. So it's exactly what you expect. Obviously you have a dual core (or dual CPU) machine - since your code will use 100% of the CPU on one core.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Beginning game programmer Petike's Avatar
    Join Date
    Jan 2008
    Posts
    64
    Yes you are right, I have a dual-core CPU (good point).

    But what now? Should I use always the GetMessage() instead of PeekMessage()? Because on many pages, there are some reasons to prefer the PeekMessage() one (I don't want to explain that reasons, laziness lol). But on the other hand I CERTAINLY don't want develop so programs which occupy 100% (or 50%, or 25%, ...) of CPU.
    So is there any solution to use "PeekMessage()" without occupying the whole CPU?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Obviously, this is a "use as appropriate". If you have other things that you need to do, but still want the application to be semi-responsive, then you use PeekMessage and call it every now and again to see if there's something that needs responding to (user does something, time event, or whatever it may be). If your application is not having something useful to do, then use GetMessage(). [that, is, it won't use 100% CPU anyways, or perform operations that "wait" in other ways, such as reading from a file, waiting for a packet on the network just to give a couple of examples].

    An alternative, where you can set a timeout for the wait, is
    MsgWaitForMultipleObjectsEx

    It allows you to wait for all sorts of different things at once, and you can replace GetMessage with that. If you have some light tasks to do (say once a second or so), you specify a timeout and if the return value is WAIT_TIMEOUT, then there was no message in that amount of time [of course, if a message comes in at the middle of the timeout, you'd preferrably calculate the amount of time passed, and reduce the timeout].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Beginning game programmer Petike's Avatar
    Join Date
    Jan 2008
    Posts
    64
    Oh,
    thank you very much.
    That helped me much.

    Petike

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Have you ever looked at CPU usage on gaming/3d modeling applications?

    World of warcraft sits at 72% usage on my system.
    Warcraft 3 sits at 48%.

    Any constant looping software is going to use a large amount of CPU cycles. On multi-core machines it really doesn't matter anyhow.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,635
    Quote Originally Posted by Raigne View Post
    Any constant looping software is going to use a large amount of CPU cycles. On multi-core machines it really doesn't matter anyhow.
    Yes, it does. It consumes power and steals cpu cycles.
    Constant looping is just wrong, game or not (when that constant looping is wasting cycles by not doing necessary stuff in the loop, that is).
    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.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,313
    [Note for mods: this thread is a windows programming topic; it has nothing to do with C++]

    The usual reason to use PeekMessage() is when there is a need to do something occasionally, in order to avoid hogging the CPU as you have done.

    Alternate ways, under windows, are;

    1) set up a timer that calls a function (which does the work you want) periodically.

    2) place whatever it is you need done into a separate thread (although this can also hog CPU time unless the thread yields control or synchonises with something else that forces it to wait sometimes).

  9. #9
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    Check the PeekMessage info at this site.

  10. #10
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,673
    >> Check the PeekMessage info at *this site*
    Horrible, misinformation on the use volatile at that site. Just a shame....

    gg

  11. #11
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    You should notify Dr. Newcomer, Microsoft MVP that he is a fountain of horrible misinformation. He also makes the same mistake in his book.

    You can reach him here

  12. #12
    Registered User
    Join Date
    Dec 2006
    Posts
    51
    Well to save looping time you can always use a sleep() function. altho if you sleep too long you might miss a few mesages, but it will definitly save some cpu power. Mine used 100% and now it roughly uses 10%. Also you might want to check if the window is active or not before you do any processing.

    But Im not an expert or anything but thats what I tried

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Shadowwoelf View Post
    Well to save looping time you can always use a sleep() function. altho if you sleep too long you might miss a few mesages, but it will definitly save some cpu power. Mine used 100% and now it roughly uses 10%. Also you might want to check if the window is active or not before you do any processing.

    But Im not an expert or anything but thats what I tried
    But it would then be better to use the MsgWaitForMultipleObjects[Ex] with a timeout value.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Constant looping is just wrong, game or not (when that constant looping is wasting cycles by not doing necessary stuff in the loop, that is).
    A game is a constant loop. This statement is absurd. A game is ALWAYS doing necessary stuff. We had this discussion before and no one could say for sure if maxed out CPU was good or bad. Now you have the issues of maxed out cores instead of just a single CPU which is another discussion. Games are going to hit the CPU and the cores very hard because they are by nature an extremely tight loop that updates and renders. I'm not even sure I would trust the CPU percentage in Windows as an accurate measure of usage. What criteria are used to compute the usage? In reality Windows is killing the CPU with tight loops since it is one huge loop with tons of other loops. It is constantly task switching which exercises the CPU. So what did they use to come up with that percentage and how reliable is it?

    If the CPU meter is basing it's measurements on how much memory or how many memory access your program is using then this is obviously severely flawed. I'm sorry but I don't trust the CPU meter because I've seen it peg out at 100% and yet the entire system remains operational and functional. Yet I've had times where XP is slow as a dog and unresponsive and the CPU meter is less than 20%. Doesn't make sense to me. It seems to me that the usage is simply a measurement based on the time slices alotted to a particular process. If 90% of the time is spent in one process then it would stand to reason that CPU usage is 90%. However if you notice the idle process in Windows is always pegged at near 95% to 99%. Obviously this is a subtraction something like: 100% - total_process_usage. So 100% usage could also mean that you are just not being friendly to other processes in the system and are hogging the CPU. And where does the kernel fit into this? Is it part of the measurements or is the CPU usage solely based on processes in the task manager? Either way it seems a moot point to me.
    Last edited by VirtualAce; 08-12-2008 at 04:30 PM.

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Bubba View Post
    A game is a constant loop. This statement is absurd. A game is ALWAYS doing necessary stuff. We had this discussion before and no one could say for sure if maxed out CPU was good or bad. Now you have the issues of maxed out cores instead of just a single CPU which is another discussion. Games are going to hit the CPU and the cores very hard because they are by nature an extremely tight loop that updates and renders. I'm not even sure I would trust the CPU percentage in Windows as an accurate measure of usage. What criteria are used to compute the usage? In reality Windows is killing the CPU with tight loops since it is one huge loop with tons of other loops. It is constantly task switching which exercises the CPU. So what did they use to come up with that percentage and how reliable is it?

    If the CPU meter is basing it's measurements on how much memory or how many memory access your program is using then this is obviously severely flawed. I'm sorry but I don't trust the CPU meter because I've seen it peg out at 100% and yet the entire system remains operational and functional. Yet I've had times where XP is slow as a dog and unresponsive and the CPU meter is less than 20%. Doesn't make sense to me. It seems to me that the usage is simply a measurement based on the time slices alotted to a particular process. If 90% of the time is spent in one process then it would stand to reason that CPU usage is 90%. However if you notice the idle process in Windows is always pegged at near 95% to 99%. Obviously this is a subtraction something like: 100% - total_process_usage. So 100% usage could also mean that you are just not being friendly to other processes in the system and are hogging the CPU. And where does the kernel fit into this? Is it part of the measurements or is the CPU usage solely based on processes in the task manager? Either way it seems a moot point to me.
    I think we are saying the same thing, but I'd like to say it my way:
    An application that DOES something is fine to use 100% CPU. But using 100% of one CPU just to check if something changed is called polling, and that should be avoided whenever possible. If a game is drawing new frames all the time [e.g. a demo or when playing the game], then I expect to see (close to) 100% CPU usage [and for a well-written game this would probably use 100% of ALL CPU's]. If on the other hand, I see 100% CPU usage when the application is doing nothing [e.g. displaying a menu, and the background is static], then I expect to see near 0% CPU usage.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Page 1 of 2 12 LastLast
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, 04: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, 05:57 PM
  5. CPU temp
    By PING in forum Tech Board
    Replies: 5
    Last Post: 01-28-2006, 05:25 AM

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