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. QThreadpool/QRunnable blocking
Forum Updated to NodeBB v4.3 + New Features

QThreadpool/QRunnable blocking

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 5 Posters 1.2k Views 2 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.
  • J Offline
    J Offline
    John Howe
    wrote on last edited by
    #1

    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.

    JonBJ 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • J Offline
        J Offline
        John Howe
        wrote on last edited by
        #3
        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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Does it block directly on the first tryStart call ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • J Offline
            J Offline
            John Howe
            wrote on last edited by
            #5

            Yes, first tryStart call.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              J 1 Reply Last reply
              0
              • J John Howe

                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.

                JonBJ Online
                JonBJ Online
                JonB
                wrote on last edited by
                #7
                This post is deleted!
                1 Reply Last reply
                0
                • SGaistS SGaist

                  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.

                  J Offline
                  J Offline
                  John Howe
                  wrote on last edited by
                  #8

                  @SGaist Execution stops after that.

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

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

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • J Offline
                      J Offline
                      John Howe
                      wrote on last edited by
                      #10

                      The application hangs.

                      1 Reply Last reply
                      0
                      • J Offline
                        J Offline
                        John Howe
                        wrote on last edited by
                        #11

                        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.

                        kshegunovK 1 Reply Last reply
                        0
                        • nageshN Offline
                          nageshN Offline
                          nagesh
                          wrote on last edited by nagesh
                          #12
                          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 ?

                          1 Reply Last reply
                          0
                          • J John Howe

                            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.

                            kshegunovK Offline
                            kshegunovK Offline
                            kshegunov
                            Moderators
                            wrote on last edited by
                            #13

                            @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.

                            Read and abide by the Qt Code of Conduct

                            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