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

QThreadpool/QRunnable blocking



  • Hello everyone,

    Still a Qt rookie. I'm trying to implement the example of the HelloWorldTask on the QThreadpool page within my existing application. The HelloWorldTask is called within a slot, executes one time, and then blocks execution thereafter. My code:

    class HelloWorldTask : public QRunnable
    {
        void run() override;
    };
    
    void HelloWorldTask::run()
    {
        qDebug() << "Hello world from thread" << QThread::currentThread();
    }
    
    void slotTask()
    {
        ...do stuff...
        HelloWorldTask *hello = new HelloWorldTask();
        pool->tryStart(hello);
        ...resume doing stuff...
    }
    

    I want the HelloWorldTask to execute every time the slot triggers. My understanding was that this would hand off the task as a worker thread and the remaining code in my slot would continue to execute, but it seems to block all execution. Do I need to call a wait somewhere? Do I need to call releaseThread? Kinda lost.


  • Lifetime Qt Champion

    Hi,

    What do you mean by block all execution ?
    How did you determine that ?
    Which version of Qt ?
    On which platform ?



  • What do you mean by block all execution ?
    

    Whereas my program normally continues running and the slotTask will be triggered almost constantly, the program hangs after the 'hello from thread x' statement.

    How did you determine that ?
    

    Output to the console window stops. Execution (and console output) would normally continue until I terminate the program.

    Which version of Qt ?
    

    5.9.3 -- not chosen by me, so I'm stuck with it.

    On which platform ?
    

    Windows 10


  • Lifetime Qt Champion

    Does it block directly on the first tryStart call ?



  • Yes, first tryStart call.


  • Lifetime Qt Champion

    How long does it block ?

    You're runnable being pretty short and quick to do, you might just see it done sooner that what you expect.



  • This post is deleted!


  • @SGaist Execution stops after that.


  • Lifetime Qt Champion

    What do you mean by stop ?
    Does your application hang or crashes ?



  • The application hangs.



  • Made a couple of tweaks and turned on some print statements in a different part of the app (a different slot triggered by QTimer).

    It almost looks like slotTask just stops printing to the console and log file.



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

    @John-Howe Does your implementation contains these statements only in run method? or any while loop ?


  • Moderators

    @John-Howe the qDebug stream is serialized internally, which is what @nagesh is alluding to, I imagine. That notwithstanding, you should just interrupt the debugger when the program hangs, and extract a complete stack trace. Upload it somewhere/post it here, so we can take a look.


Log in to reply