Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Execution freezes when sending QRunnable to QThreadpool



  • In my application, I receive data from a signal in a slot call at a rate of 60Hz. To improve performance, I'm trying to break off chunks of data and send it to be processed in various QRunnables via QThreadpool.

    Right now I am just trying to implement the Hello World task from the QThreadPool page.

    As I've got it laid out, I should be getting Hello World every time the slot is called. But instead it runs one time and then the application freezes. Any ideas?


  • Moderators

    @John-Howe said in Execution freezes when sending QRunnable to QThreadpool:

    Any ideas?

    You're ding something wrong ? 😜

    We will probably need code snippets to tell you anything remotely of value



  • From my .h:

    class HelloWorldTask : public QRunnable
    {
        void run() override;
    };
    

    Implementation:

    void HelloWorldTask::run()
    {
        qDebug() << "Hello world from thread" << QThread::currentThread();
    }
    

    Slot call (signal comes at 60Hz):

                HelloWorldTask *hello = new HelloWorldTask();
                pool->tryStart(hello);
    

  • Moderators

    @John-Howe
    works fine by me

    class HelloWorldTask : public QRunnable
    {
    
    public:
        void run() override
        {
            while (true) {
                QThread::msleep(16);//60Hz, ca.
                qDebug() << "Hello world from thread" << QTime::currentTime() << QThread::currentThread();
            }
    
        }
    };
    
    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
    //    QCoreApplication app(argc, argv);
    
        QLabel responsiveLabel("Move me");
        responsiveLabel.resize(200,200);
        responsiveLabel.show();
    
        HelloWorldTask *hello = new HelloWorldTask();
        // QThreadPool takes ownership and deletes 'hello' automatically
        QThreadPool::globalInstance()->tryStart(hello);
    
        return  app.exec();
    }
    

Log in to reply