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. QThread events not ocurring as expected
Qt 6.11 is out! See what's new in the release blog

QThread events not ocurring as expected

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 918 Views
  • 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.
  • D Offline
    D Offline
    Drazz
    wrote on last edited by Drazz
    #1

    Hello,
    I've got this code:

    worker.h

    class Worker : public QObject
    {
    public:
        Worker() {}
    
    public slots:
        void operate() {
    
            while(!QThread::currentThread()->isInterruptionRequested()) {
                qDebug() << "Operating";
            }
    
        }
    };
    

    Core.h

    class Core : public QObject
    {
    public:
        Core() {
    
            worker.moveToThread(&myThread);
    
            connect(&myThread, &QThread::started, &worker, &Worker::operate);
    
            connect(&myThread, &QThread::started, this, [=] {
               qDebug() << "thread started";
            });
    
            connect(&myThread, &QThread::finished, this, [=] {
                qDebug() << "thread finished";
            });
    
            QTimer::singleShot(1000, this, [=] {
                myThread.requestInterruption();
                myThread.quit();
            });
    
            myThread.start();
    
        }
    
    private:
        QThread myThread;
        Worker worker;
    
    };
    

    main.cpp

    #include <QCoreApplication>
    
    
    #include "core.h"
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        Core core;
    
        return a.exec();
    }
    
    

    And output is:

    Operating
    Operating
    ...
    Operating
    thread started
    thread finished
    

    I'm expecting this output:

    thread started
    Operating
    ...
    Operating
    thread finished
    

    What I'am missing?

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2

      Slots are executed in the order of connection. if you simply move connect(&myThread, &QThread::started, &worker, &Worker::operate); below connect(&myThread, &QThread::started, this, [=] { qDebug() << "thread started";}); then the output should be what you expect.

      P.S.
      Allocating worker on the stack can create problems as it can go out of scope in the main thread and get deleted while the secondary thread is still working on it

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      3
      • D Offline
        D Offline
        Drazz
        wrote on last edited by
        #3

        Worked. I was looking solution in wrong direction.

        Thanks for advice!

        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