Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QDebug prevents 100% CPU usage on boost::thread
Forum Updated to NodeBB v4.3 + New Features

QDebug prevents 100% CPU usage on boost::thread

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 332 Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Ayse
    wrote on last edited by Ayse
    #1

    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.

    0_1537336339261_printf.png
    Figure 1.

    0_1537336352928_printfw.png
    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();
    }
    
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      6

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved