question about thread

This is a discussion on question about thread within the Windows Programming forums, part of the Platform Specific Boards category; i found some program, e.g. winamp; glut library, can adjust CPU usage, dont know how to implenment that, when i ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    6

    question about thread

    i found some program, e.g. winamp; glut library, can adjust CPU usage, dont know how to implenment that, when i start a thread, the program will use 99% of CPU, but after add a Sleep(), it will be 0% and very slow. if I can adjust the CPU usage by myself, the thing will be better. any suggestion?

  2. #2
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Can we see your main loop? For most programs, it's something like:
    Code:
    while (GetMessage(...) != 0)
    {
          if (msg.message == WM_QUIT) break;
          TranslateMessage(...);
          DispatchMessage(...);
    }
    This will not use 99% CPU, as the call to GetMessage will block execution until a message is ready to be processed. What you'll need to do is call a function that blocks execution somewhere in your main loop; or call Sleep[Ex] for a short amount of time. An example of such a function i've used in the past is MsgWaitForMultipleObjects[Ex].

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    6
    sorry, i didnt explain it clearly, i mean if i do a very busy job, such as drawing random rectangles just like what Charles Petzold did in his book.
    ///////////////////////////
    while (TRUE)
    {
    if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
    {
    if (msg.message == WM_QUIT)
    break ;
    TranslateMessage (&msg) ;
    DispatchMessage (&msg) ;
    }
    else
    DrawRectangle (hwnd) ;
    }
    //////////////////////////
    it does use 100%CPU, even if i add thread, it is still 100%, add a Sleep(), it will be slowww....etc that i described them in the first post...things like that, very busy whitout thread or very slow with Sleep().
    I want my program to adjust the CPU usage automatically by using thread technology, can I?

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    Well of course its going to use 100% CPU. Look at your loop. It will loop infinitly, without stopping. It first checks if there are messages. Or else, it will draw a rectangle. Repeat. There are no waits so its gonna eat up your CPU.

    You can always change the priority of your thread but thats not going to help much if this is a game. If it's a game, its natural that it uses 100% of the CPU.

  5. #5
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Originally posted by wgan
    sorry, i didnt explain it clearly, i mean if i do a very busy job, such as drawing random rectangles just like what Charles Petzold did in his book.
    ///////////////////////////
    while (TRUE)
    {
    if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
    {
    if (msg.message == WM_QUIT)
    break ;
    TranslateMessage (&msg) ;
    DispatchMessage (&msg) ;
    }
    else
    DrawRectangle (hwnd) ;
    }
    //////////////////////////
    it does use 100%CPU, even if i add thread, it is still 100%, add a Sleep(), it will be slowww....etc that i described them in the first post...things like that, very busy whitout thread or very slow with Sleep().
    I want my program to adjust the CPU usage automatically by using thread technology, can I?
    What do you mean by "add thread"? You're drawing rectangles as fast as you can without stopping or waiting, a simple timer (like draw every 100ms) will fix this problem. Like:
    Code:
    if (lastUpdate + 100 < GetTickCount())
    {
        lastUpdate = GetTickCount();
        // Draw rectangles;
    }
    Speedy5, why do you say it's natural for games to use 99% (or there abouts) CPU? Something like this is easily fixed, why is it that you think games are some sort of exception?

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    Well any real real-time game will use 100% CPU rendering and calculating the next frame. Quake 3, Unreal, Battlezone, Medal of Honor, Descent, Operation Flashpoint, CS, Red Alert, etc. All use 100% of your CPU to make your gameplay as smooth as possible. Only games like tetris and solitare and Civilization III may not do this.

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I gather from:

    >> sorry, i didnt explain it clearly, i mean if i do a very busy job, such as drawing random rectangles just like what Charles Petzold did in his book. <<

    That you aren't making a game. The thread has really digressed if that is the case

    Anyways, I don't own the book but I assume you are using a code example from one of the lessons. It is important to note the Petzold isn't necessarily trying to teach you how to make nice programs that don't take up all the CPU bandwidth, rather he is showing you how to write different various windows apps. Books don't usually try and teach good programming style (though from what I've heard Petzold uses solid code) just how to get the job done. If this code snippet does come straight from the book would you mind telling us what the chapter is on?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Threading?
    By draggy in forum C++ Programming
    Replies: 5
    Last Post: 08-16-2005, 12:16 PM
  2. [code] Win32 Thread Object
    By Codeplug in forum Windows Programming
    Replies: 0
    Last Post: 06-03-2005, 03:55 PM
  3. How to make a thread sleep or std::recv timeout?
    By BrianK in forum Linux Programming
    Replies: 3
    Last Post: 02-26-2003, 09:27 PM
  4. Multithreading
    By Cela in forum Windows Programming
    Replies: 13
    Last Post: 01-15-2003, 02:02 PM
  5. Your Best thread and your most stupid thread ??
    By jawwadalam in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 01-03-2003, 06:41 PM

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