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. What's the best way to perform this process asynchronously using Qt's threading and concurrency features?
QtWS25 Last Chance

What's the best way to perform this process asynchronously using Qt's threading and concurrency features?

Scheduled Pinned Locked Moved General and Desktop
15 Posts 3 Posters 4.3k 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.
  • L Offline
    L Offline
    Little Red Wolf
    wrote on last edited by
    #5

    mkuettler, yes I was under the mistaken impression that Qt would marshal the remaining code in that function to the other thread, but I guess that magic isn't provided for so as you say, the remaining execution would be on the old thread. So out of curiosity, after calling moveToThread, at what point do future operations execute on the new thread? Or is that not really what it's doing?

    I will look at a signal and slots solution, sounds like the better approach.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mkuettler
      wrote on last edited by
      #6

      [quote author="Little Red Wolf" date="1340101173"]So out of curiosity, after calling moveToThread, at what point do future operations execute on the new thread? Or is that not really what it's doing?[/quote]

      The eventloop will run in the new thread, so all event handlers and slots that where connected with some kind of queued connection (the default connection mode does that for slots in different threads) will be executed in the new thread. Everything else is what you call directly, which will obviously be executed in the thread of the caller.

      1 Reply Last reply
      0
      • L Offline
        L Offline
        Little Red Wolf
        wrote on last edited by
        #7

        So mkuettler, doesn't that imply that using slots for my needs still requires me to move the object back to the Application's thread?

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mkuettler
          wrote on last edited by
          #8

          My idea was to implement a slot in the mainwindow (or in the QLineEdit itself maybe) that does the actual change. This slot would be executed in the main thread. In the query thread you would just have to emit the signal and pass the new value.

          1 Reply Last reply
          0
          • L Offline
            L Offline
            Little Red Wolf
            wrote on last edited by
            #9

            I see so the slot always executes in the thread of the object that the slot is defined on?

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mkuettler
              wrote on last edited by
              #10

              [quote author="Little Red Wolf" date="1340138548"]I see so the slot always executes in the thread of the object that the slot is defined on?[/quote]

              Yes, that is what I meant to write. Reading it again I must admit that that isn't quite clear from what I wrote.

              1 Reply Last reply
              0
              • L Offline
                L Offline
                Little Red Wolf
                wrote on last edited by
                #11

                Ok, I have this all wired up but as I mentioned I'm using the thread pool, so my class is a QRunnable. However in order to have signals, it needs to also inherit from QObject. Fine, compiles, links, great. But when I call QThreadPool::getGlobalInstance()->start(myRunnable), the run() method on my runnable never gets called. Can't see why. Any ideas?

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  mkuettler
                  wrote on last edited by
                  #12

                  I'm not sure what the problem is. But if you have other QRunnables already running in the QThreadPool it could be that maxThreadCount() threads are already running, and the new thread is just appended to the ready queue, waiting for one of the other threads to terminate.

                  1 Reply Last reply
                  0
                  • L Offline
                    L Offline
                    Little Red Wolf
                    wrote on last edited by
                    #13

                    mkuettler, I have no other runnables, it's the only one. My runnable also inherits from QObject in order to support signals, could that be a problem? How does one go about debugging this?

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      mkuettler
                      wrote on last edited by
                      #14

                      I find it very hard to guess what the problem might be, as my knowledge about threads is very limited. Could you maybe post some of your code here?

                      You might also want to read "Threads, Events and QObjects":http://qt-project.org/wiki/Threads_Events_QObjects. I havn't read it myself yet, but it seems to be a good read on that topic.

                      1 Reply Last reply
                      0
                      • K Offline
                        K Offline
                        KA51O
                        wrote on last edited by
                        #15

                        Another good read on threads and the recommended approach for using QThreads is "this one":http://qt-project.org/wiki/QThreads_general_usage.

                        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