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. What difference between while(true) and QTimer?
Forum Updated to NodeBB v4.3 + New Features

What difference between while(true) and QTimer?

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 5 Posters 3.6k 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #2

    Hi,

    Your timer runs at a fixed interval.

    An infinite loop runs as fast as it can (but will block the event loop).

    Either use a smaller interval for your timer or implement a thread that will use your infinite loop.

    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
    4
    • I Offline
      I Offline
      isan
      wrote on last edited by
      #3

      @SGaist When start timer without the interval.like:

      tm->start();
      

      Is the interval 0?if yes, how can set smaller interval?

      mrjjM 1 Reply Last reply
      0
      • I isan

        @SGaist When start timer without the interval.like:

        tm->start();
        

        Is the interval 0?if yes, how can set smaller interval?

        mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #4

        @isan
        Hi
        It cannot be smaller than 1 ms.
        tm->start(1);

        If you need it faster, a thread - as suggested is the way to go.

        https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/

        JKSHJ 1 Reply Last reply
        2
        • mrjjM mrjj

          @isan
          Hi
          It cannot be smaller than 1 ms.
          tm->start(1);

          If you need it faster, a thread - as suggested is the way to go.

          https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/

          JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote on last edited by JKSH
          #5

          @isan said in What difference between while(true) and QTimer?:

          When start timer without the interval.like:

          tm->start();
          

          Is the interval 0?

          The interval used here is the one reported by tm->interval(). http://doc.qt.io/qt-5/qtimer.html#start-1

          The default interval value is 0. You can change it by calling setInterval().

          @isan said in What difference between while(true) and QTimer?:

          When I do this with while(true) in function ,I get more numbers than when do this with QTimer that connected to read function
          I start a timer in mainWindow

          connect(tm, SIGNAL(timeout()),this, SLOT(readFunction()));
               tm->start();
          

          why?

          Because a timer with a 0 interval yields the CPU when your slot finishes running. This allows the CPU to do other things between reading your ADC. (see https://en.wikipedia.org/wiki/Yield_(multithreading) ).

          while(true) {...} does not yield unless you call sleep() inside the loop. There is a chance it will consume 100% of the CPU core and never let other threads use it.

          Also, there is a bit of overhead when using signals and slots (but I don't know how much). You can try to reduce the overhead by using the new signal-slot syntax (see http://doc.qt.io/qt-5/signalsandslots-syntaxes.html ):

          connect(tm, &QTimer::timeout, this, &MyClass::readFunction); // replace "MyClass" with your real class name
          

          I don't want to use while(true) in my code ,what should I do to ,read more numbers at a faster rate?

          What rate do you want?

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          aha_1980A I 2 Replies Last reply
          5
          • JKSHJ JKSH

            @isan said in What difference between while(true) and QTimer?:

            When start timer without the interval.like:

            tm->start();
            

            Is the interval 0?

            The interval used here is the one reported by tm->interval(). http://doc.qt.io/qt-5/qtimer.html#start-1

            The default interval value is 0. You can change it by calling setInterval().

            @isan said in What difference between while(true) and QTimer?:

            When I do this with while(true) in function ,I get more numbers than when do this with QTimer that connected to read function
            I start a timer in mainWindow

            connect(tm, SIGNAL(timeout()),this, SLOT(readFunction()));
                 tm->start();
            

            why?

            Because a timer with a 0 interval yields the CPU when your slot finishes running. This allows the CPU to do other things between reading your ADC. (see https://en.wikipedia.org/wiki/Yield_(multithreading) ).

            while(true) {...} does not yield unless you call sleep() inside the loop. There is a chance it will consume 100% of the CPU core and never let other threads use it.

            Also, there is a bit of overhead when using signals and slots (but I don't know how much). You can try to reduce the overhead by using the new signal-slot syntax (see http://doc.qt.io/qt-5/signalsandslots-syntaxes.html ):

            connect(tm, &QTimer::timeout, this, &MyClass::readFunction); // replace "MyClass" with your real class name
            

            I don't want to use while(true) in my code ,what should I do to ,read more numbers at a faster rate?

            What rate do you want?

            aha_1980A Offline
            aha_1980A Offline
            aha_1980
            Lifetime Qt Champion
            wrote on last edited by
            #6

            What rate do you want?

            and what do you want to do with the ADC samples?

            Qt has to stay free or it will die.

            I 1 Reply Last reply
            0
            • JKSHJ JKSH

              @isan said in What difference between while(true) and QTimer?:

              When start timer without the interval.like:

              tm->start();
              

              Is the interval 0?

              The interval used here is the one reported by tm->interval(). http://doc.qt.io/qt-5/qtimer.html#start-1

              The default interval value is 0. You can change it by calling setInterval().

              @isan said in What difference between while(true) and QTimer?:

              When I do this with while(true) in function ,I get more numbers than when do this with QTimer that connected to read function
              I start a timer in mainWindow

              connect(tm, SIGNAL(timeout()),this, SLOT(readFunction()));
                   tm->start();
              

              why?

              Because a timer with a 0 interval yields the CPU when your slot finishes running. This allows the CPU to do other things between reading your ADC. (see https://en.wikipedia.org/wiki/Yield_(multithreading) ).

              while(true) {...} does not yield unless you call sleep() inside the loop. There is a chance it will consume 100% of the CPU core and never let other threads use it.

              Also, there is a bit of overhead when using signals and slots (but I don't know how much). You can try to reduce the overhead by using the new signal-slot syntax (see http://doc.qt.io/qt-5/signalsandslots-syntaxes.html ):

              connect(tm, &QTimer::timeout, this, &MyClass::readFunction); // replace "MyClass" with your real class name
              

              I don't want to use while(true) in my code ,what should I do to ,read more numbers at a faster rate?

              What rate do you want?

              I Offline
              I Offline
              isan
              wrote on last edited by
              #7

              @JKSH when read number with while(true) I get 120 numbers per second
              and with QTimer I get 40 numbers per second
              so for now ok to get 120 numbers persecond

              JKSHJ 1 Reply Last reply
              0
              • aha_1980A aha_1980

                What rate do you want?

                and what do you want to do with the ADC samples?

                I Offline
                I Offline
                isan
                wrote on last edited by isan
                #8

                @aha_1980 I want to draw a chart with numbers and when the numbers lost the chart is not valid enough
                I know if change i2c baud rate the sample rate is change ,but also its important how to read

                mrjjM 1 Reply Last reply
                0
                • I isan

                  @aha_1980 I want to draw a chart with numbers and when the numbers lost the chart is not valid enough
                  I know if change i2c baud rate the sample rate is change ,but also its important how to read

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #9

                  @isan
                  Hi
                  I think you will be most happy if you spend the extra time and
                  make a tread to do the reading as it wont block main and will
                  be as fast as while(true).

                  1 Reply Last reply
                  3
                  • I Offline
                    I Offline
                    isan
                    wrote on last edited by
                    #10

                    @mrjj said in What difference between while(true) and QTimer?:

                    I think you will be most happy if you spend the extra time and
                    make a tread to do the reading as it wont block main and will
                    be as fast as while(true).

                    I do this surely. tank you

                    1 Reply Last reply
                    0
                    • I isan

                      @JKSH when read number with while(true) I get 120 numbers per second
                      and with QTimer I get 40 numbers per second
                      so for now ok to get 120 numbers persecond

                      JKSHJ Offline
                      JKSHJ Offline
                      JKSH
                      Moderators
                      wrote on last edited by
                      #11

                      @isan said in What difference between while(true) and QTimer?:

                      @JKSH when read number with while(true) I get 120 numbers per second
                      and with QTimer I get 40 numbers per second
                      so for now ok to get 120 numbers persecond

                      120 samples per second with while(true) is quite slow. A QTimer should be able to reach that rate, so I don't think that CPU yielding is the reason for the slowness.

                      1. Can you show more code?
                      2. What does tm->interval() return?
                      3. What ADC is it?
                      4. Where is the analogRead() function from?

                      @isan said in What difference between while(true) and QTimer?:

                      @aha_1980 I want to draw a chart with numbers

                      Drawing a chart is very expensive. I agree with @mrjj -- you should see an improvement if you read the ADC in a dedicated thread and draw the chart in the GUI thread.

                      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                      1 Reply Last reply
                      5
                      • I Offline
                        I Offline
                        isan
                        wrote on last edited by isan
                        #12

                        @JKSH I dont use tm->interval()

                        ADC is PCF8591 with 100k scl

                        I use wiringpi library ,that has <pcf8591.h>

                        I try to use reading analog pin in thread tnx.

                        #include "chart.h"
                        #include <QtCharts/QAbstractAxis>
                        #include <QtCharts/QSplineSeries>
                        #include <QtCharts/QValueAxis>
                        #include <QtCore/QDebug>
                        #include <wiringPi.h>
                        #include <pcf8591.h>
                        #define PCF       120
                        
                        Chart::Chart(QGraphicsItem *parent, Qt::WindowFlags wFlags):
                            QChart(QChart::ChartTypeCartesian, parent, wFlags),
                            m_series(0),
                            m_axis(new QValueAxis),
                            m_step(0),
                            m_x(5),
                            m_y(1)
                        {
                            QObject::connect(&m_timer, &QTimer::timeout, this, &Chart::handleTimeout);
                            m_timer.setInterval(1000);
                        
                            tm=new QTimer(this);
                             connect(tm, SIGNAL(timeout()),this, SLOT(readFun()));
                             tm->start();
                            m_series = new QSplineSeries(this);
                            QPen green(Qt::red);
                            green.setWidth(3);
                            m_series->setPen(green);
                            m_series->append(m_x, m_y);
                        
                            addSeries(m_series);
                            createDefaultAxes();
                            setAxisX(m_axis, m_series);
                            m_axis->setTickCount(5);
                            axisX()->setRange(0, 10);
                            axisY()->setRange(60,180);
                            wiringPiSetup () ;
                                // Setup pcf8591 on base pin 120, and address 0x48
                             pcf8591Setup (PCF, 0x48) ;
                        
                            m_timer.start();
                        }
                        
                        1 Reply Last reply
                        0

                        • Login

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