Results 1 to 15 of 15

Thread: QThread and QEventLoop - Idle Processing

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2010
    Posts
    7
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    2

    Default Re: QThread and QEventLoop - Idle Processing

    Quote Originally Posted by wysota View Post
    If you want to do idle processing in the thread besides the main task the thread is doing, use a timer instead of sleep().
    In the initial post of the op, I understood the idle processing is the main task. (Polling the device is the only task of the thread, and it must be done at idle time.) In that case, is its timer-based solution (re-coded below) appropriate?

    Qt Code:
    1. class DevicePoller : public QThread
    2. {
    3. Q_OBJECT
    4. QTimer m_timer;
    5. slots:
    6. void poll();
    7. protected:
    8. virtual void run()
    9. {
    10. connect( m_timer, SIGNAL( timeout() ), this, SLOT( poll() ) );
    11. m_timer.start( 0 );
    12. exec();
    13. }
    14. };
    To copy to clipboard, switch view to plain text mode 

    Or, is it best to avoid timers at all, and launch a thread with QThread::IdlePriority:

    Qt Code:
    1. class DevicePoller : public QThread
    2. {
    3. protected:
    4. virtual void run()
    5. {
    6. while( !finished ) poll();
    7. }
    8. };
    9. DevicePoller mythread;
    10. mythread.start( QThread::IdlePriority );
    11. // ...
    To copy to clipboard, switch view to plain text mode 

    Or, am I wrong and there is another strategy?

    Thanks for the rapid answer anyway!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: QThread and QEventLoop - Idle Processing

    Quote Originally Posted by jlemaitre View Post
    In the initial post of the op, I understood the idle processing is the main task. (Polling the device is the only task of the thread, and it must be done at idle time.) In that case, is its timer-based solution (re-coded below) appropriate?
    It's a periodic task, not idle processing.

    Qt Code:
    1. class DevicePoller : public QThread
    2. {
    3. Q_OBJECT
    4. QTimer m_timer;
    5. slots:
    6. void poll();
    7. protected:
    8. virtual void run()
    9. {
    10. connect( m_timer, SIGNAL( timeout() ), this, SLOT( poll() ) );
    11. m_timer.start( 0 );
    12. exec();
    13. }
    14. };
    To copy to clipboard, switch view to plain text mode 
    There is no point in using threads here, you can use the main thread with this approach.

    Or, is it best to avoid timers at all, and launch a thread with QThread::IdlePriority:

    Qt Code:
    1. class DevicePoller : public QThread
    2. {
    3. protected:
    4. virtual void run()
    5. {
    6. while( !finished ) poll();
    7. }
    8. };
    9. DevicePoller mythread;
    10. mythread.start( QThread::IdlePriority );
    11. // ...
    To copy to clipboard, switch view to plain text mode 

    Or, am I wrong and there is another strategy?
    Thread priorities are just hints for the OS, it may ignore them so that's not a good approach -- you'll end up with a busy loop.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    jlemaitre (28th April 2011)

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
  •  
Qt is a trademark of The Qt Company.