PDA

View Full Version : Multithreaded spend CPU 100%



wisconxing
18th December 2008, 03:46
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:


void Thread::run()
{
while (!stopped)
printf("%s",&messageStr);
std::cerr << qPrintable(messageStr);
stopped = false;
std::cerr << std::endl;
}

caduel
18th December 2008, 08:03
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