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. How to process events during infinite loop in Worker thread
Forum Updated to NodeBB v4.3 + New Features

How to process events during infinite loop in Worker thread

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 7.1k Views 1 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
    armandomartins
    wrote on last edited by
    #1

    I've created a Worker object in Qt to process video input indefinitely, and then move it into a QThread to keep the UI thread going. The problem is, I designed it such that the video capture function runs in an infinite loop until interrupted by a flag, and the flag was supposed to be set by a slot in the Worker object, but since the Worker object is inside the infinite loop, it never processes this "quit" slot (or at least I think that's what's happening). I depend on an external library so substituting the video polling for another method is not really an option. Can someone please confirm that this is indeed the problem and suggest a solution? Here's the code:

    class worker : public QObject{
        Q_OBJECT
    public:
        worker(QObject* parent = NULL);
        ~worker(){}
        Q_SLOT void process();
        Q_SLOT void stop();
    private:
        bool quit;
    };
    
    worker::worker(QObject *parent) : QObject(parent){
        quit = false;
    }
    void worker::process(){
        while(!quit){
            //this library call puts the thread to sleep until a frame is available
            WaitForVideoFrame();
        }
    }
    void worker::stop(){
        quit = true;
    }
    

    and then from the UI object I have:

    MyWorker = new worker();
    QThread* thread = new QThread;
    MyWorker->moveToThread(thread);
    connect(thread, SIGNAL(started()), MyWorker, SLOT(process()));
    QPushButton* stop_button = new QPushButton(this);
    connect(stop_button, SIGNAL(clicked(bool)), MyWorker, SLOT(stop()));
    thread->start();
    

    The problem here is that when I press the stop_button, nothing happens, the worker keeps running the loop. Is there maybe a function that I can call to yield processing time to the event loop from within the infinite loop? Or a better design/solution for this? Any suggestions are welcome.

    JKSHJ 1 Reply Last reply
    0
    • A armandomartins

      I've created a Worker object in Qt to process video input indefinitely, and then move it into a QThread to keep the UI thread going. The problem is, I designed it such that the video capture function runs in an infinite loop until interrupted by a flag, and the flag was supposed to be set by a slot in the Worker object, but since the Worker object is inside the infinite loop, it never processes this "quit" slot (or at least I think that's what's happening). I depend on an external library so substituting the video polling for another method is not really an option. Can someone please confirm that this is indeed the problem and suggest a solution? Here's the code:

      class worker : public QObject{
          Q_OBJECT
      public:
          worker(QObject* parent = NULL);
          ~worker(){}
          Q_SLOT void process();
          Q_SLOT void stop();
      private:
          bool quit;
      };
      
      worker::worker(QObject *parent) : QObject(parent){
          quit = false;
      }
      void worker::process(){
          while(!quit){
              //this library call puts the thread to sleep until a frame is available
              WaitForVideoFrame();
          }
      }
      void worker::stop(){
          quit = true;
      }
      

      and then from the UI object I have:

      MyWorker = new worker();
      QThread* thread = new QThread;
      MyWorker->moveToThread(thread);
      connect(thread, SIGNAL(started()), MyWorker, SLOT(process()));
      QPushButton* stop_button = new QPushButton(this);
      connect(stop_button, SIGNAL(clicked(bool)), MyWorker, SLOT(stop()));
      thread->start();
      

      The problem here is that when I press the stop_button, nothing happens, the worker keeps running the loop. Is there maybe a function that I can call to yield processing time to the event loop from within the infinite loop? Or a better design/solution for this? Any suggestions are welcome.

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

      @armandomartins said in How to process events during infinite loop in Worker thread:

      since the Worker object is inside the infinite loop, it never processes this "quit" slot (or at least I think that's what's happening).

      Yes, that's exactly what's happening. Event loops (which process signals/slots) and infinite loops are incompatible with each other; you cannot have both in the same thread.

      I depend on an external library so substituting the video polling for another method is not really an option. Can someone please confirm that this is indeed the problem and suggest a solution?

      In this case:

      1. Don't use a worker QObject + moveToThread(). Instead, subclass QThread and put your infinite loop inside your override of QThread::run().
      2. From the main thread, call QThread::requestInterruption() when you want to quit. (You don't need to implement your own Quit function)
      3. Replace while (!quit) {...} with while (!isInterruptionRequested()) {...}

      This should also significantly simplify your code.

      See:

      • https://doc.qt.io/qt-5/qthread.html#requestInterruption
      • https://doc.qt.io/qt-5/qthread.html#isInterruptionRequested

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

      1 Reply Last reply
      5

      • Login

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