CPU Usage and Win32

This is a discussion on CPU Usage and Win32 within the C++ Programming forums, part of the General Programming Boards category; It seems whenever I run/make a win32 program, it always uses up 100% of the cpu, at least thats what ...

  1. #1
    Chess Master Phanster's Avatar
    Join Date
    Oct 2004
    Posts
    8

    CPU Usage and Win32

    It seems whenever I run/make a win32 program, it always uses up 100% of the cpu, at least thats what the Ctrl-Alt-Del menu in windows XP shows. Is there something wrong?

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,701
    >> Is there something wrong?
    If it's not doing something usefull, then yes.

    gg

  3. #3
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,904
    More than likely you're using PeekMessage instead of GetMessage.

  4. #4
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    That happens because of your message loop. If you switch PeekMessage() with GetMessage(), then your CPU usage will drop back to normal levels.. although if you want stuff to happen during the time when there are no messages available, this might not be possible. Alternately, a quick-fix solution is to add a Sleep(1); line to your message loop. I believe this tells the OS basically to 'give up the rest of the CPU time slice', if I remember correctly from wherever I read it. But the real reason it works might also just be because the time spent sleeping (1ms) is hugely greater than the time spent on processing the rest of the contents of the loop - I'm no expert on such things Also try putting your message loop in a separate thread with idle priority or something.

    If someone has a better solution, I'd be glad to hear it too
    Last edited by Hunter2; 10-26-2004 at 06:14 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  5. #5
    Chess Master Phanster's Avatar
    Join Date
    Oct 2004
    Posts
    8
    Thanks alot, it reduces my crazy CPU usage. One problem though, I can't close the window from the top right

  6. #6
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    See if your function to handle messages parses WM_QUIT, WM_DESTROY and WM_CLOSE to end your aplication (calling exit or whatever).

  7. #7
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,904
    Hunter2---Sleep(0) gives up the time slice, Sleep(1) waits a millisecond. I believe that is the way it works.

    yah, from msdn:

    Quote Originally Posted by MSDN
    A value of zero causes the thread to relinquish the remainder of its time slice to any other thread of equal priority that is ready to run. If there are no other threads of equal priority ready to run, the function returns immediately, and the thread continues execution.

  8. #8
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Sleep(1) is usually used to give up the rest of the time slice as Sleep(0) only yields to threads of equal or higher priority, leaving lower priority threads dead in the water. Sleep(1) will yield to any ready thread.

  9. #9
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    While I don't know about the exact behaviour of Sleep(), I do know that Sleep(0) does not drop my CPU usage below 100%. For more info, see anonytmouse's post
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. string::operand+ usage problem (with WIN32 data types)
    By flashbaz-pi in forum C++ Programming
    Replies: 17
    Last Post: 11-02-2008, 02:41 PM
  2. win32 timer function
    By Anuradh_a in forum Windows Programming
    Replies: 2
    Last Post: 03-04-2008, 11:51 AM
  3. Function that get CPU Idle
    By daskmaster in forum Windows Programming
    Replies: 5
    Last Post: 02-16-2003, 09:13 AM

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