Results 1 to 2 of 2

Thread: Multithreaded spend CPU 100%

  1. #1
    Join Date
    Nov 2008
    Posts
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Multithreaded spend CPU 100%

    I run C++ GUI Qt 4 programing(Editor 2)14th Multithreaded example Threads, found when a thread started, the CPU be ueed up 100% performance. It may be caused in thread::run() while loop. How to reduce the CPU spending? The Thread::run() see below:

    Qt Code:
    1. void Thread::run()
    2. {
    3. while (!stopped)
    4. printf("%s",&messageStr);
    5. std::cerr << qPrintable(messageStr);
    6. stopped = false;
    7. std::cerr << std::endl;
    8. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jpn; 18th December 2008 at 15:45. Reason: missing [code] tags

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Multithreaded spend CPU 100%

    That thread does print an endless sequence of some message to stdout/err.
    There is no sleep or anything to slow it down.
    So, yes, this program (ok: this thread) will eat us much cpu as it can get.

    (This is just a demo program, so I would ignore that here. Basically, one common use case for threads is when you have some computation intensive (or otherwise long lasting) task. This task needs cpu time. The threads do not eliminate that need, but rather keep your program (gui) working while that computation is taking place.)

    HTH

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.