Solved QDebug prevents 100% CPU usage on boost::thread
-
In code blocks , if I have no QDebug or printf on threads I get Figure 1, with QDebug I get sinusoid diagram on Figure 2. But I want to CPU usage like Figure 1, what caused this unexpected CPU usage , how can I prevent sinusoid CPU usage.
Figure 1.
Figure 2.Thanks Regards..
boost::mutex mtx; static int i =0; void tFunc() { while(true) { mtx.lock(); ++i; mtx.unlock(); // qDebug() = comment => CPU %100? qDebug() << i; } } void tFunc2() { while(true) { mtx.lock(); ++i; mtx.unlock(); qDebug() << i; } } int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); cpu_set_t set; // cpu_set_t : Data structure represents a set of CPUs. CPU_ZERO(&set); //CPU_ZERO : Clears set, so that it contains no CPUs. CPU_SET(0,&set); // CPU 1 //CPU_SET : Add CPU to cpu set. sched_setaffinity(0,sizeof(cpu_set_t),&set); boost::thread *tP2 = new boost::thread(tFunc); boost::thread *tP3 = new boost::thread(tFunc2); return a.exec(); }
-
Hi,
It's not unexpected, printing something from your thread requires a lot more instructions and IO than just incrementing a counter that will happen in memory.
So if you want to maximise your CPU usage, don't print.