What's the best way to perform this process asynchronously using Qt's threading and concurrency features?
-
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.
-
[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.
-
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?
-
-
I see so the slot always executes in the thread of the object that the slot is defined on?
-
[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.
-
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?
-
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.
-
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?
-
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.
-
Another good read on threads and the recommended approach for using QThreads is "this one":http://qt-project.org/wiki/QThreads_general_usage.